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

来源:互联网 发布:剑桥美国经济史知乎 编辑:程序博客网 时间:2024/05/20 12:21
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;    }

0 0
原创粉丝点击