当前位置: 移动技术网 > IT编程>开发语言>c# > C# read file to bytes,File.ReadAllFiles,File.Open(),BinaryReader

C# read file to bytes,File.ReadAllFiles,File.Open(),BinaryReader

2019年12月09日  | 移动技术网IT编程  | 我要评论
using system;
using system.text;
using system.io;

namespace consoleapplication15
{
    class program
    {
        static void main(string[] args)
        {
            string filename = @"..\..\images\lj.jpg";
            filestreamreadtobytes(filename);
            readfiletobinarybytes(filename);
            filereadallbytesdemo(filename);
            console.readline();
        }

        static void filestreamreadtobytes(string filename)
        {
            if(!file.exists(filename))
            {
                return;
            }

            byte[] bytesarr = null;
            using (filestream fs = new filestream(filename, filemode.open))
            {
                bytesarr = new byte[fs.length];
                fs.write(bytesarr, 0, bytesarr.length);
                console.writeline(bytesarr.length);
            }
        }

        static void filereadallbytesdemo(string filename)
        {
            byte[] readbytes = system.io.file.readallbytes(filename);
            console.writeline(readbytes.length);
        }

        static void readfiletobinarybytes(string filename)
        {
            if (!file.exists(filename))
            {
                return;
            }

            byte[] bytesarr = null;
            using (filestream fs = new filestream(filename, filemode.open))
            {
                using (binaryreader binreader = new binaryreader(fs,encoding.utf8))
                {
                    bytesarr = binreader.readbytes((int)fs.length);
                    console.writeline(bytesarr.length);
                }                    
            }
        }
    }    
}

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网