Monday, August 29, 2016

How to write to a Text File in ASP.NET

In this article I will explain how to write to a text file using two methods,
one using File class and the second one using StreamWriter class.

Using File Class :


This class has methods to write to a text file which are WriteAllText and
WriteAllLines and it has methods to append text to a file named AppendAllText
and AppendAllLines.

Below you can find the sample code :

C# Code :



using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class WriteText : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnWrite_Click(object sender, EventArgs e)
    {
        File.WriteAllText(@"d:\test.txt","Hello how are you?");
    }
}

WriteAllText method creates a text file in the specified path, writes the string
mentioned and then closes the file. If file already exists it will overwrite that file.

Another sample is :

        string[] arr = { "Think Good", "Do Good", "Speak Good" };
        File.WriteAllLines(@"d:\test.txt",arr);

WriteAllLines method creates a text file in the specified path, writes the specified 
strings of the array in separate lines and then closes the file. If file already exists it 
will overwrite that file.

WriteAllBytes method is also similar to the above method but it is used for writing
byte array.

Next is the example of code for appending text to an existing file.

File.AppendAllText(@"d:\test.txt","How do you do?");

AppendAllText method opens the file, append the specified string to that file and
then closes the file. If file doesn't exist, it will create a new one.

Next is the example of code for appending lines to an existing file.

  string[] lines = { "Think Good", "Speak Good" };
  File.AppendAllLines(@"d:\test.txt", lines);

AppendAllLines method opens the file, append the specified strings of the array in
separate lines and then closes the file. If file doesn't exist, it will create a new one.

This method exists from .NET framework 4.0.

Using StreamWriter Class :


StreamWriter  writes characters to a stream in a particular encoding. It has
WriteLine and Write methods for writing to a file.

WriteLine  method writes the specified string  followed by a line terminator to the
text stream

Write method just writes the specified string to the stream.

Below is the sample code :

C# Code :



using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class WriteText : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnStreamWrite_Click(object sender, EventArgs e)
    {
       using (StreamWriter sr = new StreamWriter(@"d:\test.txt"))
        {
            sr.WriteLine("Think Good, Do Good, Speak Good");
            sr.Write("Hai How are you?");
            sr.Write("Hello Hope you are fine.");
           
        }
    }
}

Output will be like this when you open the text file :

Think Good, Do Good, Speak Good
Hai How are you?Hello Hope you are fine.

You can notice that there is no line separator in second line between
two strings "Hai How are you?" and "Hello Hope you are fine."

StreamWriter is used with using statement in the above code
as using statement will ensure that Dispose method is called
which releases unmanaged resources.

No comments:

Post a Comment