Writing and Reading Text File using C#


Introduction

Reading and Writing to text file is common requirement of programmers. C# provides a convenient way to work with text files. .NET Framework includes convenience classes that make reading and writing text files very easy. Actually, to write (create) or read files, there is three certain steps we need to follow.

(i) Opening the file
(ii) Read or Write (create new file) file
(iii) Close the file

The best way to understand working with files is to jump right into the code, let's jump.

Writing File

using System;
using System.IO;
class WritingFile
{
    static void Main(string[] args)
    {
        //opening a file stream to write text
        TextWriter tW = new StreamWriter("page.txt");
        //write the line to file
        tW.WriteLine("text goes here");
        //close the stream
        tW.Close();
    }
}

In above code, 'System' namespace is base or say primary namespace for applications and 'System.IO' namespace is used to read or write the file.
Now, to open the file I have used

TextWriter tW = new StreamWriter("page.txt");

Above code will open or say create the new text file, if file already exist then it will replace by the new file.

Next, to write some lines in page.txt file, I have used

tW.WriteLine("text goes here");

Above code will write quoted text in page.txt file.
Finally, at the end I have closed the stream by using

tW.Close();

That's all about the writing of file.

Note: You can check the new file in bin directory on project root; by default in Visual Studio editor this directory is hidden.

Let's proceed next to read the file.

Reading File

using System;
using System.IO;
class ReadFile
{
    static void Main(string[] args)
    {
        //opening a file stream to read text
        TextReader tR = new StreamReader("page.txt");
        //reading line and printing to console
        Console.WriteLine(tR.ReadLine());
        Console.ReadKey();
        //close the stream
        tR.Close();
    }
}

In above code, I have opened the text reader stream by file name and in next line printing that line to console. Here we may use either

Console.WriteLine(tR.ReadLine());

Or

Console.WriteLine(tR.ReadToEnd());

First one will read single line but the second one will read entire texts in the file.

We have one other way to do all this things without stream opening or closing. Let's look at that way too.

using System;
using System.IO;
class ReadFileAndWriteFile
{
    static void Main(string[] args)
    {
        if (File.Exists("page.txt"))
        {
            Console.WriteLine("Current content of file:");
            string getTexts = File.ReadAllText("page.txt");
            Console.WriteLine(getTexts);
            Console.ReadKey();
        }
        else
        {
            Console.WriteLine("Please enter new content for the file:");
            string putTexts = Console.ReadLine();
            File.WriteAllText("page.txt", putTexts);
        }
    }
}

In above code, if file exits then it will read the content of file and in case file does not exist then it will create a new one and place the text, if you run that program next time then it will display what you have typed.

Comments

Popular posts from this blog

Migrating database from ASP.NET Identity to ASP.NET Core Identity

Customize User's Profile in ASP.NET Identity System

Lambda two tables and three tables inner join code samples