Deleting a Text File

In this lesson, you will learn how to delete a file from a folder by using the My.Computer.FileSystem Object.

You can delete a text file by using the My.Computer.FileSystem.DeleteFile Method. It is always a good idea to ask users if they are sure they want to delete a file before you actually delete it. Imagine if you had accidentally clicked a button that automatically deleted a file that you needed without warning! You will use the MsgBoxResult Enumeration to determine whether the user clicks Yes or No when asked to confirm the deletion.

Try It!

To delete a file

  1. Open the Picture Viewer project from the previous lesson. If you have not yet completed it, go to Reading from a Text File and finish that lesson before you continue.

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

  3. Add a Button control to the form, positioning it next to the Load Favorites button.

  4. Change the following properties of this button:

    Property

    Value

    Name

    DeleteFavorites

    Text

    Delete Favorites

    Size

    92, 23

  5. Double-click the new Button control to add the default event handler in the Code Editor.

  6. In the DeleteFavorites_Click event handler, add the following code. This code checks to make sure that the file to be deleted exists, and then displays a message box to verify that the user wants to delete the file. If the user clicks yes, the picture box and list box are cleared, and then the FavoritePictures text file is deleted from the Documents folder.

    ' Check that the favorites text file exists.
    If My.Computer.FileSystem.FileExists(FavoritePictures) Then
    
        ' Ensure that user wants to delete the favorites text file.
        If MsgBox("Are you sure you want to send the favorites" &
              " file to the Recycle Bin?", MsgBoxStyle.YesNo, 
            "Delete Favorite Pictures") = MsgBoxResult.Yes Then
    
            ' Clear the picture box and the list box.
            Me.ListBox1.Items.Clear()
            Me.PictureBox1.ImageLocation = ""
    
            ' Delete the favorites file.
            My.Computer.FileSystem.DeleteFile(FavoritePictures)
    
        End If
    Else
        MsgBox("The favorites file does not exist.")
    End If
    
  7. Press F5 to run the code.

  8. Click Delete Favorites and then click Yes when you are prompted.

  9. Verify that the FavoritePictures.txt file has been deleted from the Documents folder by clicking the Load Favorites button.

Next Steps

In this set of lessons, you learned how to create a Picture Viewer application that enables you to read file names from the Pictures directory and display the corresponding pictures in a picture box. You also learned how to read from and write to a text file and how to delete the text file. In the next set of lessons, you will learn about classes, the blueprints for objects that you can reuse in your programs.

Next Lesson: Programming with Objects: Using Classes

See Also

Tasks

Retrieving the Names of Files in a Folder

Writing to a Text File

Reading from a Text File

Other Resources

Using the File System: Writing to and Reading from Files

Visual Basic Guided Tour