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


 
  

Create file

 
 
       
Introduction :
This tutorial explain to create and write content into text (.txt) using C#, Vb.Net

In this tutorial we will see how to create text file. .NET framework provides few classes to deal with file operation all these classes are available under System.IO namespace.

Let's create text file

using C# code sample. in this example we have use FileStream to create File and StreamWriter class to write content into newly created file.

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

StreamWriter MyStreamWriter = new StreamWriter(fileStream);

try

{

MyStreamWriter.Write("This is my first line");

MyStreamWriter.Write("This is my Second line");

}

catch (Exception ex)

{

throw ex;

}

finally

{

MyStreamWriter.Flush();

MyStreamWriter.Close();

}

Using vb.net Sample

Dim fileStream As New FileStream("c:\myFile.txt", FileMode.OpenOrCreate)

Dim MyStreamWriter As New StreamWriter(fileStream)

Try

MyStreamWriter.Write("This is my first line")

MyStreamWriter.Write("This is my Second line")

Catch ex As Exception

Throw ex

Finally

MyStreamWriter.Flush()

MyStreamWriter.Close()

End Try

 

Other Tutorials in the same Category:
      
  • Create file
  •  
           
     
    Return to Main Index Page


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