Writing to a Text File

In this lesson, you will learn how to write a string to a text file in Visual Basic by using the My.Computer.FileSystem Object.

The previous lesson, Retrieving the Names of Files in a Folder, describes how to create a Picture Viewer application that displays pictures that are in a folder in a picture box. The next step is to save the names of your favorite pictures to a text file.

File Basics

Before you write to a text file, there are some things that you should know. The My object in Visual Basic makes working with files easy. You can write to files on a computer by using the My.Computer.FileSystem Object.

You can use the My.Computer.FileSystem.WriteAllText Method to write a string to a text file. If the file does not exist, this method will create it for you. You can indicate that you want to add text to the file, instead of overwriting existing text, by passing True as the append parameter.

Note

In Windows XP, there are special folders named MyDocuments, MyPictures, and MyMusic. In Windows Vista and Windows 7, these folders do not have "My" in the names; instead, they are named Documents, Pictures, and Music. You will use these folders in this lesson. However, the code in this lesson will run on both Windows XP and Windows Vista.

The tasks in this lesson show how you can sequentially access text files. While you can also use random access to read or write to a text file, you might instead want to consider using a database. You can only use random access for text files if you know the exact length of the file and of each line in that file.

Try It!

To append text to a text file

  1. Open the Picture Viewer project created in the Retrieving the Names of Files in a Folder topic. If you have not yet completed it, see Retrieving the Names of Files in a Folder and finish that lesson before you continue.

  2. In Solution Explorer, select Form1.vb, and then, on the View menu, click Designer.

  3. Add a Button control to the form, next to the Load Pictures button, and name it FavoritesAdd.

  4. Change the following properties of this button:

    Property

    Value

    Text

    Add to Favorites

    Size

    92, 23

  5. Double-click the Add to Favorites button, and add the following code to the FavoritesAdd_Click event handler. This code adds the text selected in the list to the FavoritePictures.txt file.

    If PictureBox1.ImageLocation <> "" Then
    
        ' Add the selected picture to the favorites text file.
        My.Computer.FileSystem.WriteAllText(FavoritePictures, 
            Me.ListBox1.SelectedItem & ",", True)
    
    End If
    
  6. Add the following code above the FavoritesAdd_Click event handler. This code defines the path and file name for the FavoritePictures variable.

    Dim FavoritePictures As String = 
        My.Computer.FileSystem.SpecialDirectories.MyDocuments &
          "\FavoritePictures.txt"
    
  7. Press F5 to run the code.

  8. Click Load Pictures and then click your favorite picture in the list.

  9. Click Add to Favorites.

  10. Open the FavoritePictures.txt file saved in the Documents folder to verify that the name of the picture is appended to the file.

Next Steps

In this lesson, you learned how to append text to a text file. In the next lesson, you will learn how to read text from a text file.

Next Lesson: Reading from a Text File

See Also

Tasks

Retrieving the Names of Files in a Folder

Other Resources

Using the File System: Writing to and Reading from Files

Visual Basic Guided Tour

Change History

Date

History

Reason

July 2011

Added a paragraph about random access text files.

Customer feedback.