C# (Mono) – Reading and writing to a text file

C# (Mono) on FreeBSD
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.

8 Comments

  1. اخبار تامین اجتماعی شرق تهران و خبرهای فوری فارس و اخبار
    ورزشی زنده فوتبال و معلومات های مفید و دانستنی های
    یک مهندس عمران و خبرهای جدید ذبیح الله مجاهد

    https://bookmarklogin.com/story13597682/اخبار-ورزشی-بانوان-شبکه-۳

  2. Rhyous says:

    I should update this to use the using keyword.

            public static string ReadEntireFile(string inFile)
            {
                string fileAsString = string.Empty;
    
                // Open the file into a StreamReader
                using (StreamReader file = File.OpenText(inFile))
                {
                    // Read the file into a string
                    fileAsString = file.ReadToEnd();
    
                }
                // Return the string
                return fileAsString;
            }
    
  3. Saran says:

    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

  4. Casper says:

    i guess the ".txt" file will normally export to the "Debug" folder where the path is "...\bin\Debug"

  5. Jack says:

    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

  6. Chris says:

    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

Leave a Reply to Rhyous

How to post code in comments?