Search code examples
c#windowsfilebinarybinarystream

Create a File From Binary Data In C#


I am trying to read a file in binary and create a file of the same format with that binary, this file can be in any commom format like (.docx, .txt, .jpeg, .png ) etc.

I created a text file (test.txt) that has a string This is a text string written in it.

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

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

Now how do i create a file of the same extension with the binary data. ?


Solution

  • Per comment feedback, this is more about encryption than just binary file I/O.

    Encryption can be done on a file after it is already saved:

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

    And for basic guidance on working with encryption and files in C#, I'd suggest this Microsoft example walkthrough:

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