Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
~[VB.NET]~ [TUT] How To Write To/Read A Text File
#1
How to write to a text file


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.

Code:
Sponsored by
DevExpress Free UI Controls
Become a Sponsor
Similar ArticlesMost ReadTop RatedLatest
    External Source Directive in VB.NET
    WPF Printing in VB.NET
    PathGeometry in Silverlight
    Geometry Mini-Language in WPF
    Rectangle and RectangleGeometries in WPF
More...
    Working with Strings in VB.NET
    Displaying data in a DataGrid in VB.NET
    Creating an Excel Spreadsheet programmatically using VB.NET
    Exception Handling in VB.NET
    Creating a SQL Server database programmatically using VB.NET
More...
    A Simple Web Service in VB.NET
    Flash Player Custom Control for ASP.NET 2.0
    Saving and reading Objects
    Tic Tac Toe Game in VB.NET
    Drawing transparent Images and Shapes using Alpha Blending
More...
    Exception Handling in VB.NET
    WrapPanel control in WPF using VB.NET
    TextBox control in WPF using VB.NET
    How to Set Focus on a Control in ASP.NET using VB.NET
    How to Upload Files in ASP.NET using VB.NET
More...


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

How to read a text file


We can use StreamReader to read text files. The following code snippet reads a file and writes on the console line by line.

Code:
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

I Hope That Will Help Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to hook the keyboard. [VB.NET] [TuT] Vorfin 92 60,993 09-23-2019, 11:55 AM
Last Post: Jamimartin
  VB.NET Port Scanner [TUT] Fragma 30 14,477 11-27-2012, 11:26 PM
Last Post: ƃu∀ ıʞƃu∀
  [TUT] MD5 Encrypter & Finder [VB.NET] Fragma 12 7,858 11-25-2012, 10:36 PM
Last Post: ƃu∀ ıʞƃu∀
  [TUT] How to make a simple WebBrowser in VB 2010! [TUT] - [ Pictures] Statics 95 61,972 10-07-2012, 06:56 AM
Last Post: a99
  [SF Coders] Grabbing an Icon from a File and Saving! [TUT] milopeach 13 3,816 05-15-2012, 06:22 PM
Last Post: OnyxDev

Forum Jump:


Users browsing this thread: 2 Guest(s)