C# 文件转字节数组 byte[]

public byte[] fileTobyte(string filePath)
    {
        byte[] buffer;
        using (FileStream fs = File.OpenRead(filePath))
        {
            buffer = new byte[fs.Length];

            byte[] b = new byte[1024];
            //UTF8Encoding temp = new UTF8Encoding(true);
            int i = 0;
            while (fs.Read(b, 0, b.Length) > 0)
            {
                for (int j = 0; j < 1024; j++)
                {
                    if (i * 1024 + j < buffer.Length)
                    {
                        buffer[i * 1024 + j] = b[j];
                    }
                }
                i++;
            }
            fs.Close();
            fs.Dispose();
        }
        return buffer;
    }

你可能感兴趣的:(C# 文件转字节数组 byte[])