Save file from a byte[] in C# NET 3.5

荒凉一梦 提交于 2019-12-03 00:24:53
Christian Rodemeyer

Use the static void System.IO.File.WriteAllBytes(string path, byte[] bytes) method.

byte[] buffer = new byte[200];
File.WriteAllBytes(@"c:\data.dmp", buffer);
public static void SaveFile(this Byte[] fileBytes, string fileName)
{
    FileStream fileStream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite);
    fileStream.Write(fileBytes, 0, fileBytes.Length);
    fileStream.Close();
}
Erich Mirabal

In addition to what everyone else has already stated, I would also suggest you use 'using' clauses since all those objects implement IDisposable.

using(FileStream outFileStream = new ...)
using(ZOutputStream outZStream = new ...)
using(FileStream inFileStream = new ...)
{
    CopyStream(inFileStream, outZStream);
}

Stick the byte array you received into a MemoryStream and compress/decompress it on the fly without using temporary files.

You can try this code

 private void t1()
    {
        FileStream f1 = new FileStream("C:\\myfile1.txt", FileMode.Open);
        int length = Convert.ToInt16(f1.Length);
        Byte[] b1 = new Byte[length];
        f1.Read(b1, 0, length);
        File.WriteAllBytes("C:\\myfile.txt",b1);
        f1.Dispose();
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!