Wednesday, March 14, 2012

CREATE A TEXT FILE AND WRITE IN IT C# EXAMPLE

In C# creating and writing on a Text file is really easy as compared to C, C++. Below is the code written is Visual Studio 2010 which creates a file called "abc.txt" in D drive, and than write some string in it finally we save that string in the file.

You will need to add a System.IO namespace on the top of your program in order to successfully run this code

1:   StreamWriter objstreamwriter = new StreamWriter(@"D:\abc.txt", true); //specify path for the file, true indicate weather to overwrite the file if it already exist  
2:   objstreamwriter.WriteLine("Write this string in the Text file located at D drive");  
3:    objstreamwriter.Flush();  


Points to note:
  • StreamWriter is the class which we are using to create File
  • objstreamwriter is the object of class StreamWriter
  • WriteLine is the method of StreamWriter class which write text and at the end automatically escape a line i.e. Enter
  • Flush() is a method that we used to save the text in the file
If you have any queries please post comment.

Thanks

    No comments:

    Post a Comment