View Single Post
  #3 (permalink)  
Old 08-25-2007, 02:40 AM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Wink Re: How to Read and write txt file using C#

We can read the file using File class as follows,

Code:
//Read a txt file

string[] str = File.ReadAllLines(@"C:/test.txt");
if (str.Length > 0)
 {
   for (int i = 0; i < str.Length; i++)
   {
     MessageBox.Show(str[i]);
   }
 }

We can write the file using StreamWriter as follows,

Code:
//Write a txt file

StreamWriter sw = new StreamWriter("C:/test.txt");
string str = "Test for writing";
sw.WriteLine(str);
sw.Close();
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Reply With Quote