C# 本地文件以byte字节流的方式读取


C# 实现本地文件怎么转换成byte[] 进行读取操作

public static byte[] GetBytes(string fileName)
        {
            FileStream pFileStream = null;
            byte[] pReadByte = new byte[0];
            try
            {
                pFileStream = new FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite, System.IO.FileShare.ReadWrite);
                BinaryReader r = new BinaryReader(pFileStream);
                r.BaseStream.Seek(0, SeekOrigin.Begin);    //将文件指针设置到文件开始
                pReadByte = r.ReadBytes((int)r.BaseStream.Length);
                return pReadByte;
            }
            catch
            {
                return pReadByte;
            }
            finally
            {
                if (pFileStream != null)

                    pFileStream.Close();
            }

        }

文章地址: C# 本地文件以byte字节流的方式读取 | Yuan天空博客

相关内容