-2

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. ?

Huzaifa
  • 345
  • 4
  • 15
  • 1
    Which specific bit are you confused by? Getting the file extension? Adding the file extension to a new filename? Writing binary data to the new file? Something else? – canton7 Dec 15 '20 at 15:06
  • Writing the data to a new file... – Huzaifa Dec 15 '20 at 15:06
  • 1
    `File.WriteAllBytes`? – canton7 Dec 15 '20 at 15:06
  • 3
    I remain always a bit perplexed by these questions. They are not very common but neither very rare. If you have a method called ReadAllBytes it takes no great immagination to think about the existance of a WriteAllBytes. So the culprit is always the same. [RTFM](https://en.wikipedia.org/wiki/RTFM) – Steve Dec 15 '20 at 15:11
  • 2
    Is that all you're doing? Wouldn't copying it be easier? https://learn.microsoft.com/en-us/dotnet/api/system.io.file.copy?view=net-5.0 – Dan Csharpster Dec 15 '20 at 15:47
  • 1
    @DanCsharpster I am actually trying to encrypt it and save it with the same format and also with a function to decrypt. – Huzaifa Dec 15 '20 at 15:51
  • 2
    Oh okay. You could let .NET encryption encrypt it for you after you've already saved it to disk. https://learn.microsoft.com/en-us/dotnet/api/system.io.file.encrypt?view=net-5.0 – Dan Csharpster Dec 15 '20 at 15:56
  • 2
    Also, following this walkthrough might be helpful. https://learn.microsoft.com/en-us/dotnet/standard/security/walkthrough-creating-a-cryptographic-application – Dan Csharpster Dec 15 '20 at 15:56
  • 2
    @DanCsharpster Thanks, that was what I needed. – Huzaifa Dec 15 '20 at 16:03
  • Cool! I would suggest updating your question to reflect that, then, to help others. I can post this as the answer too. – Dan Csharpster Dec 15 '20 at 18:28

2 Answers2

1

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

Dan Csharpster
  • 2,662
  • 1
  • 26
  • 50
0

may be this could be helpful

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