Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将字节数组转换为文件_C#_File_Bytearray - Fatal编程技术网

C# 将字节数组转换为文件

C# 将字节数组转换为文件,c#,file,bytearray,C#,File,Bytearray,我有一个文件(D:/D.txt),我正在将其转换为字节数组,然后用RC4加密该数组 string filename="D:/d.txt" byte[] buff = null; FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); Bi

我有一个文件(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;
但是现在我想将数组转换回文件,我如何才能做到这一点

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


只需打开一个文件并将其写入该文件。您应该在
使用
块的
fs
中使用,而不是关闭您打开以读取该文件的文件流。您的所有代码也可以替换为
return File.ReadAllBytes(“D:/D.txt”)