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);
Related
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);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have six txt files. And one of them holding the others names in inside. For example i have a txt file called brands. And inside of brands there are names of cars brands for example: bmv, toyota, audi...
And also there are files who exactly same names (bmv.txt, toyota.txt, audi.txt).
enter image description here
(Sorry for my english its not my native language)
My questions is can i create arrays for each car using for loop?
Although this question is rather vague, I will give you a basic flow for what you need to do.
Read File names for each category and store them.
Then Read the Contents of each category and match the values with the filenames.
Read Contents of each sub category file.
You are going to use the following Code Samples to Achieve this.
Get Filenames in Directory:
string [] fileEntries = Directory.GetFiles(targetDirectory);
Read Contents of File:
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
string line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
This is a basic approach that should get you started.
Good Luck.
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 5 years ago.
Improve this question
In c# How many way to open file? Which one is best? and how to open .exe file? Sorry for silly question but i am new in c#.
using (StreamReader srStreamReader = new StreamReader(sString))
{
while ((sline = srStreamReader.ReadLine()) != null)
{
Console.WriteLine(sline);
}
}
I am use this code for this but am not able. so please help
If I understood the problem correctly
You can use like this
string path;
byte[] bufferArray = File.ReadAllBytes(path);
string base64EncodedString = Convert.ToBase64String(bufferArray );
bufferArray = Convert.FromBase64String(base64EncodedString );
File.WriteAllBytes(path, bufferArray );
By open file do you mean execute it or read line by line?
If execute then probably something like this is the answer:
Process.Start("C:\\");
From the code you've provided, it looks like you want to be able to view the source of an .exe. This can't be done without using a decompiler and knowing what the application was compiled with.
If you're trying to execute the .exe file, then take a look at the static method System.Diagnostics.Process.Start(filePath).
If you're trying to actually read the contents, you can use ILSpy or other similar software to decompile the application to view source. ILSpy has source available on GitHub, so you'll be able to use that to get the contents you want.
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
I want to open a Resource File with the FileStream Class. It's a text File and I want to read it Line by Line.
FileStream fs = new FileStream(Properties.Resources.Testing, FileMode.Open, FileAccess.Read);
The called Exception is System.ArgumentException and it says there is an invalid character.
I hope anyone can help me to fix this, or if theres a better way it's also ok but I need the File in the .exe so it needs to be a Resource..
When you add a text file as a resource, it will be embedded as a string. So your FileStream constructor call will assume that you are trying to open a file on disk with a name that is the same as the text file content. That ends poorly of course.
It isn't very clear if you really want a stream, the string tends to be good as-is, you might consider the String.Split() method to break it up into lines. Or maybe you like the StringReader class so you can use ReadLine():
using (var rdr = new StringReader(Properties.Resources.Testing)) {
string line;
while ((line = rdr.ReadLine()) != null) {
// Do something with line
//...
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I created an application which has 2 labels and 1 textbox.
The first label will show the total number of days since the year began, while the second shows the number of tests in the year. In the textbox shows the number of tests that I have not studied.
When the program opens, you have to click a button to load an .txt file to get these numbers, they are saved when you exit by clicking in another button. I Wanted to know how to load the file and to save the parameters.
I'm developing in C#, grateful, sorry for bad english.
To read text use File.ReadAllLines
List<string> lines = File.ReadAllLines(path).ToList();
To write text use File.WriteAllLines
File.WriteAllLines(path, lines.ToArray());
Or some variation on those methods to suit your needs.
One approach would be this:
using System.IO;
using (var reader = new StreamReader(fileName))
{
string line;
while ((line = reader.ReadLine()) != null)
{
// Do stuff with your line here, it will be called for each
// line of text in your file.
}
}
Or, you could do:
List<String> txtStrings = File.ReadAllLines(path).ToList();
Or, come up with your own approach by reading up on this post.