C# 将字节数组转换为文件
我有一个文件(D:/D.txt),我正在将其转换为字节数组,然后用RC4加密该数组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
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”)代码>