C# 读写文件

来源:互联网 发布:scala 下载 linux版本 编辑:程序博客网 时间:2024/06/05 10:47

读文件到byte[]的函数:

    public static bool readFile(string filePathName, out byte[] bytes)    {        FileStream stream = new FileStream(filePathName, FileMode.Open);        bool ret = false;        bytes = null;        if (null != stream)        {            int len = (int)stream.Length;            bytes = new byte[len];            int readLend = stream.Read(bytes, 0, len);            stream.Flush();            stream.Close();            ret = readLend == len;        }        return ret;    }

写byte[]到文件

 private void write2File(string filePathName, byte[] bytes)    {        if(File.Exists(filePathName))        {            File.Delete(filePathName);        }        FileStream stream = new FileStream(filePathName, FileMode.Create);        stream.Write(bytes, 0, bytes.Length);        stream.Flush();        stream.Close();         }



0 0
原创粉丝点击