Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleRead and write all of the lines in a text file in Visual Basic .NET
DescriptionThis example shows how to read and write all of the lines in a text file in Visual Basic .NET. It uses the System.IO.File.ReadAllLines and System.IO.File.WriteAllLines methods.
Keywordssort, IComparer, file, compare, Visual Basic .NET
CategoriesFiles and Directories
 
This example doesn't really do much that's useful. It's just intended to remind you that the ReadAllLines and WriteAllLines methods exist.

The System.IO.File class provides methods to read and write all of the lines in a file to and from an array of strings. (This really should have been included in the My namespace along with My.Computer.FileSystem.ReadAllText and My.Computer.FileSystem.WriteAllText.)

When you click the program's ReadLines button, the following button reads the file's lines into the array all_lines. It then displays them in a ListBox just so you can see that something happened.

 
Private Sub btnReadLines_Click(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    btnReadLines.Click
    Dim all_lines() As String = _
        System.IO.File.ReadAllLines(txtInputFile.Text)
    lstItems.DataSource = all_lines
End Sub
 
When you click the Write Lines button, the following code calls the WriteAllLines method to save the lines in the ListBox into an output file.
 
Private Sub btnWriteLines_Click(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    btnWriteLines.Click
    System.IO.File.WriteAllLines(txtOutputFile.Text, _
        lstItems.DataSource)
    Beep()
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated