【问题标题】:Create a File From Binary Data In C# [closed]在 C# 中从二进制数据创建文件 [关闭]
【发布时间】:2021-03-26 05:46:24
【问题描述】:

我正在尝试读取二进制文件并创建一个与该二进制文件格式相同的文件,该文件可以是任何常见格式,如(.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.....

现在我如何使用二进制数据创建具有相同扩展名的文件。 ?

【问题讨论】:

标签: c# windows file binary binarystream


【解决方案1】:

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

可以对已保存的文件进行加密:

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

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

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

【讨论】:

    【解决方案2】:

    这可能会有所帮助

    // 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); 
    

    【讨论】:

      猜你喜欢
      • 2013-12-13
      • 1970-01-01
      • 2012-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-08
      相关资源
      最近更新 更多