ARTICLE

How to write to a text file in VB.NET

Posted by Mahesh Chand Articles | Visual Basic 2010 July 30, 2010
This code sample shows how to write to a text file in C#.
Download Files:
 
Reader Level:

We can StreamWriter class to write to a file. The Write method is used to write to a text file.

In the StreamWriter constructor, the first parameter is the file name and second parameter is optional, boolean type. If second parameter is true, an existing file will be appended with new text. Otherwise it will erase old text and start with new text.

 

Module Module1

 

    Sub Main()

 

 

        Dim fileName As String = "C:\Mahesh\McTextFile.txt"

        Using reader As New System.IO.StreamReader(fileName)

            While Not reader.ReadLine() Is Nothing

                Console.WriteLine(reader.ReadLine())

            End While

        End Using

 

        Try

            Using writer As New System.IO.StreamWriter(fileName, True)

                writer.WriteLine("New text")

                writer.WriteLine("New text line goes here")

            End Using

        Catch ex As Exception

 

        End Try

 

        Console.ReadKey()

 

    End Sub

 

End Module

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor