C# (Mono) – Reading and writing to a text file
File access is made simple with C# (Mono) on FreeBSD.
Reading a text file with C# (Mono)
To read a file, use a StreamReader
object. However, it is easy if you don’t do a new StreamWriter("File.txt")
but instead use File.OpenText("File.txt")
to create the StreamReader
. Once you have the stream, you just need to run the stream’s ReadToEnd()
function.
Here is a snippet to read a text file as a string.
// Open the file into a StreamReader StreamReader file = File.OpenText("SomeFile.txt"); // Read the file into a string string s = file.ReadToEnd();
Now that you have the text file as a string, you can manipulate the string as you desire.
Writing to a text file with C# (Mono)
To write a text file, use StreamWriter.
string str = "Some text"; // Hook a write to the text file. StreamWriter writer = new StreamWriter("SomeFile.txt"); // Rewrite the entire value of s to the file writer.Write(str);
You can also just add a line to the end of the file as follows:
string str = "Some text"; // Hook a write to the text file. StreamWriter writer = new StreamWriter("SomeFile.txt"); // Rewrite the entire value of s to the file writer.WriteLine(str);
Example for learning
An example of these in a little project file made with MonoDevelop.
using System; using System.IO; namespace FileAccess { class MainClass { public static void Main (string[] args) { string FileName="TestFile.txt"; // Open the file into a StreamReader StreamReader file = File.OpenText(FileName); // Read the file into a string string s = file.ReadToEnd(); // Close the file so it can be accessed again. file.Close(); // Add a line to the text s += "A new line.\n"; // Hook a write to the text file. StreamWriter writer = new StreamWriter(FileName); // Rewrite the entire value of s to the file writer.Write(s); // Add a single line writer.WriteLine("Add a single line."); // Close the writer writer.Close(); } } }
Well, this should get you started.
اخبار تامین اجتماعی شرق تهران و خبرهای فوری فارس و اخبار
ورزشی زنده فوتبال و معلومات های مفید و دانستنی های
یک مهندس عمران و خبرهای جدید ذبیح الله مجاهد
https://bookmarklogin.com/story13597682/اخبار-ورزشی-بانوان-شبکه-۳
I should update this to use the using keyword.
i had a "New.txt" file in c folder.I mentioned the path of that file at string FileName="C:/programfiles/New.txt". after executing that i am not getting the text in that file>how can i get that text read and displays it on the output window?Can anyone explain please
i guess the ".txt" file will normally export to the "Debug" folder where the path is "...\bin\Debug"
Hey,
Thanks for the short quid.
i wanted where shell i place the txt file because mine wont find it and if its possible to export the info of a txt file to mysql.
thanks in advanced
Your Example for learning is MUCH faster this way. You don't need to read the entire file into a variable in memory to append text to it.
https://gist.github.com/1683548
I know...but the example is supposed to show both reading and writing. 🙂