.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> File Operation >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Write to .txt/Text file

Posted By:Pankaj Mishra       Posted Date: August 17, 2008    Points: 10    Category: File Operation    URL: http://www.dotnetspark.com  
 

Hi,

Below is one of the way to read .txt/text file using C#. In this code i have used StreamWriter and FileStream class provided by .NET Framework.

        FileStream fileStream = new FileStream(@"c:\myFile.txt", FileMode.OpenOrCreate);

try
{
        StreamWriter streamWriter = new StreamWriter(fileStream);

        streamWriter.BaseStream.Seek(0, SeekOrigin.End);
        streamWriter.Write(" Write anything you want");
      
}
catch(Exception ex)

{
  throw ex;
}
finally
{
 streamWriter.Flush();
 streamWriter.Close();
}
 

Cheers
Pankaj


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend