Create a File From Binary Data In C# [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
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. ?

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

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

Related

Detecting if the file is CSV without checking extension in C# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
How can I check if a file that is in a directory is a CSV file without checking its extension?
If the file extension is not a mandatory for your program design, but only format of the file (csv in your case), the only valid way to check if a given file is either "ok" for you or not, is simply to check your data structure after you populated it from the file.
The basic flow in gross may look like this:
1) Select file
2) Read the file
a) Exception happens somewhere = non valid CSV formatted file
b) All is ok
3) Validate datastructure(s) populated from the file
a) Some mandatory fields are not initialized = non valid CSV file
b) All is ok

Include image as a part of text file? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Is it possible to add image( signature captured from client on windows Mobile 6.5 device and saved as a file with jpeg extension)
to text file using vb.net/C#?
objective:
We were trying to include the signature of client from jpeg file to just below Received By section of a text file
===================================
Hotel XYZ
No.26 abc road ....
***********************************
xyzsdafdsaf
Tokyo 28
Japan 55555555
Date : 10/27/2014
Received by
[SignatureImage.jpg]
No, there is no way to put anything but text in a text file. If you want it to be visibly formatted as both text and an image, you will need to output it in a file format which supports both text and images. For instance, you could use PDF or RTF.

Why does not read the whole text file? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
In C#, I would like to read a .tga picture into a string variable.
I use many of variations to read from a text file, but there is a problem with every solution.
The file size 17Kb
Why doesn't read the whole text?
For example this does not work:
string item = "";
while ((item = sr.ReadLine()) != null)
{
picture_string += sr.ReadLine()+"";
}
It does not work:
picture_string = sr.ReadToEnd();
It does not work
picture_string = File.ReadAllText(path);
The file you are trying to read is a binary file, not a text file. Stop trying to read a binary file as if it is a text file.
var fileContents = File.ReadAllBytes(path);

How can i build string variable of the next file should be created from the known last file? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
This is the last file:
last_file = fi[fi.Length -1].FullName;
In this case the last_file contain the directory and the file name:
C:\Users\bout0_000\AppData\Local\mws\My Weather Station\radar_temp_directory\radar010286.gif
Now i want to create what should be the next file:
string nextfile =
File.Copy(combinedTemp, nextfile);
combinedTemp is allways the file name untitled.gif
I want to copy this file to C:\Users\bout0_000\AppData\Local\mws\My Weather Station\radar_temp_directory\
As radar010287.gif
Assuming fi contains all files named radarNNNNNN.gif where N is 0-9 and the list is ordered, you have a few steps to do:
// Obtain the file with the highest number, but only the filename, not the path
string lastFile = fi[fi.Length - 1].Name;
// Take the number part of the filename
string lastFileNumber = lastFile.Substring(5, 6);
// Parse it as int
int lastNumber = int.Parse(lastFileNumber);
// Increase it by one
lastNumber++;
// Build a new filename
string newFileName = string.Format("radar{0:D6}.gif", lastNumber);
// And make it a full path again by prepending the directory name
newFileName = Path.Combine(radarImagesDirectory, newFileName);
Of course you'll have to include some error handling and include code to make this work when no files are present in the directory.

Converting file content to string and store in sharepoint column [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I have a requirement to allow user to upload any file (pdf, doc, ect) to sharepoint and later on download the file as needed. the upload and download tasks are implemented as asp.net web app. Currently, I convert the file content (byte array) to base64 string and store in sharepoint. For download, I get the content from sharepoint and do convert from base64 to string then I write this string to browser. The result did not look like original file. What is wrong?
From your question it sounds like the problem is you are converting the Base64 data to a string. Since Base64 is a representation of binary data using only printable characters it works fine as a string, but interpreting binary data directly as a string can result in data corruption. It's like trying to edit an EXE file using Notepad.
You need to convert the Base64 back to a byte array and write that to the browser.

Categories