Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.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语言从二进制数据创建文件#_C#_Windows_File_Binary_Binarystream - Fatal编程技术网

C# 用C语言从二进制数据创建文件#

C# 用C语言从二进制数据创建文件#,c#,windows,file,binary,binarystream,C#,Windows,File,Binary,Binarystream,我试图读取一个二进制文件并创建一个与该二进制文件格式相同的文件,该文件可以是任何常用格式,如(.docx、.txt、.jpeg、.png)等 我创建了一个文本文件(test.txt),其中包含一个字符串,这是一个写入其中的文本字符串 string file = "filelocation"; //this has a file location byte[] data = File.ReadAllBytes(fileWithExt); // this gi

我试图读取一个二进制文件并创建一个与该二进制文件格式相同的文件,该文件可以是任何常用格式,如
(.docx、.txt、.jpeg、.png)

我创建了一个文本文件
(test.txt)
,其中包含一个字符串
,这是一个写入其中的文本字符串

string file = "filelocation";    //this has a file location 

byte[] data = File.ReadAllBytes(fileWithExt);     //  this gives me binary data.....

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

这可能会有帮助

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

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

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

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


这些问题总是让我有些困惑。它们不是很常见,但也不是很罕见。如果您有一个名为ReadAllBytes的方法,那么考虑WriteAllBytes的存在不需要太多的想象。所以罪魁祸首总是一样的。你就这么做了吗?复制不是更容易吗?哦,好的。您可以在将其保存到磁盘后,让.NET encryption为您进行加密。此外,遵循本演练可能会有所帮助@谢谢,这正是我需要的。