代码

/// <summary>
    /// 将文件转换成byte[] 数组
    /// </summary>
    /// <param name="fileUrl">文件路径文件名称</param>
    /// <returns>byte[]</returns>
    public byte[] AuthGetFileData(string fileUrl)
    {
        using (FileStream fs = new FileStream(fileUrl, FileMode.Open, FileAccess.Read))
        {
            byte[] buffur = new byte[fs.Length];
            fs.Read(buffur, 0, (int)fs.Length);
            return buffur;
        }
    }

 2

string filePath = @"path\to\your\file.txt";
        using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
        {
            BinaryReader br = new BinaryReader(fs);
            byte[] fileBytes = br.ReadBytes((int)fs.Length); // 读取整个文件
            Console.WriteLine("File read successfully.");
        }

string filePath = @"path\to\your\file.txt";
        byte[] fileBytes = File.ReadAllBytes(filePath);

 

posted on 2018-10-17 12:48  邢帅杰  阅读(1329)  评论(0)    收藏  举报