C# 读写文件

读文件到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();
     
    }



猜你喜欢

转载自blog.csdn.net/konglingbin66/article/details/51496579