从C#中的二进制数据创建文件



我正在尝试读取二进制文件,并创建一个与该二进制文件格式相同的文件,该文件可以是任何通信格式,如(.docx, .txt, .jpeg, .png )等。

我创建了一个文本文件(test.txt),其中写入了一个字符串This is a text string

string file = "filelocation";    //this has a file location 
byte[] data = File.ReadAllBytes(fileWithExt);     //  this gives me binary data.....

现在,我如何创建一个具有相同扩展名的二进制数据文件?

根据评论反馈,这更多的是关于加密,而不仅仅是二进制文件I/O。

可以在文件保存后对其进行加密:

https://learn.microsoft.com/en-us/dotnet/api/system.io.file.encrypt?view=net-5.0

关于在C#中使用加密和文件的基本指导,我建议微软进行以下示例演练:

https://learn.microsoft.com/en-us/dotnet/standard/security/walkthrough-creating-a-cryptographic-application

这可能有助于

// Specifing a file name 
var path = @"file.txt"; 
// Specifing a new file name 
var nPath = @"file2.txt"; 
// Calling the ReadAllBytes() function 
byte[] data = File.ReadAllBytes(path); 
// Calling the WriteAllBytes() function 
// to write the file contents with the 
// specified byte array data 
File.WriteAllBytes(nPath, data); 

最新更新