将字节数组转换为文件

时间:2014-12-06 13:57:17

标签: c# file bytearray

我有一个文件(D:/d.txt),我正在转换为字节数组,然后用RC4加密数组

 string filename="D:/d.txt"
    byte[] buff = null;
    FileStream fs = new FileStream(fileName, 
                                   FileMode.Open, 
                                   FileAccess.Read);
    BinaryReader br = new BinaryReader(fs);
    long numBytes = new FileInfo(fileName).Length;
    buff = br.ReadBytes((int) numBytes);
    return buff;

但现在我想将数组转换回文件,我该怎么做呢

1 个答案:

答案 0 :(得分:1)

试试这个:

  string filename = "D:/d.txt";
    byte[] buff = null;
    FileStream fs = new FileStream(filename, 
                                   FileMode.Open, 
                                   FileAccess.Read);
    BinaryReader br = new BinaryReader(fs);
    long numBytes = new FileInfo(filename).Length;
    buff = br.ReadBytes((int) numBytes);


File.WriteAllBytes("Foo.txt", buff);

//    or

File.WriteAllBytes("Foo.txt", buff.ToArray());

文档

  

System.IO.File.WriteAllBytes - MSDN