Home > Code > File Handling > Create a text file and Read/Write using StreamWriter and StreamReader in C#.NET

Create a text file and Read/Write using StreamWriter and StreamReader in C#.NET

by Thamil   on Dec 19, 2011   Category: File Handling   |  Views: 1307    |  Points: 25   |  Gold 


Below C# code will help you to create text file and write into text file also how we can read the text line by line using StreamWriter and StreamReader


Namespace

using System;
using System.IO;


C# Code

private void WriteRead()
{
try
{
using (StreamWriter StrWriter = new StreamWriter(@"C:\test.txt"))
{
StrWriter.WriteLine("Create a text");
StrWriter.WriteLine("file and Read/Write using");
StrWriter.WriteLine("StreamWriter and StreamReader");
StrWriter.WriteLine("in C#.NET");
StrWriter.Write(StrWriter.NewLine);
StrWriter.Close();
}
using (StreamReader StrReader = new StreamReader(@"C:\test.txt"))
{
string ReadLine = string.Empty;
while ((ReadLine = StrReader.ReadLine()) != null)
{
Response.Write(ReadLine+"<br>");
}
StrReader.Close();
}
}
catch (Exception e)
{
Response.Write(e.Message);
}





Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts