In this section, we will see reading and writing simple files with C #. Fortunately for us, C # makes it very easy. The file class from Syste.IO namespace, we come as much as possible, with very easy everything, making it easy to read and write a file easy.
In our first example, we will produce an extremely minimalist text editor. Actually, it is so easy that we can only read one file and then write it new content, and at the same time a line of text is available. But it shows how easy it is to use the file class:
using System;
using System.IO;
namespace FileHandlingArticleApp
{
class Program
{
static void Main(string[] args)
{
if(File.Exists("test.txt"))
{
string content = File.ReadAllText("test.txt");
Console.WriteLine("Current content of file:");
Console.WriteLine(content);
}
Console.WriteLine("Please enter new content for the file:");
string newContent = Console.ReadLine();
File.WriteAllText("test.txt", newContent);
}
}
}
You will see that we use the file class in three places: we use it to use it to check whether our file exists or not, we use the ReadAllText () method to read the contents of the file , And we use WriteAllText () method to write new content. You will see that I am not using the full path, but only a simple file name is this file Will be kept as the executable file in the same directory, which is right now. In addition, the example should be easy to understand: we check the file, if it exists, we read its contents and output it to the console. Then we prompt the user for new material, and once we have it, we write it in the file. Obviously it will overwrite the previous content, but for now, it's okay. We can use the AppendAllText method instead. Try changing the WriteAllText line instead:
File.AppendAllText("test.txt", newContent);
If you run it, you will see that the new text is added to the existing text instead of overwriting it. As simple as that. But we still get only one line of text per execution of our application. Let's be a bit creative and change that. Replace the last lines in our example with this:
Console.WriteLine("Please enter new content for the file - type exit and press enter to finish editing:");
string newContent = Console.ReadLine();
while(newContent != "exit")
{
File.AppendAllText("test.txt", newContent + Environment.NewLine);
newContent = Console.ReadLine();
}
As you can see, when we want to stop editing the file, the user gives instructions to remove the word, and unless they do this, we attach the user input to the file and then For a new line, we indicate a new line, environment to look like the actual lines of the text. New lines also add.
However, instead of writing files every time, a more beautiful solution might look like this:
Console.WriteLine("Please enter new content for the file - type exit and press enter to finish editing:");
using(StreamWriter sw = new StreamWriter("test.txt"))
{
string newContent = Console.ReadLine();
while(newContent != "exit")
{
sw.Write(newContent + Environment.NewLine);
newContent = Console.ReadLine();
}
}
The use of currents is slightly more than the scope of this chapter, but the good thing about this in this example is that we open a file only once, and once again we are satisfied then write changes in it. In this case, we are taking advantage of the use of the C # statement, which ensures that the file reference closes after the scope once, when it has a block of {if you say () statement If you do not use, you need to manually call the stop () method on the Streiver Whistler instance.