Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
Visual Basic
 Deleting a Text File
Collapse All/Expand All Collapse All
Visual Basic Guided Tour
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.

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.

    Visual Basic
    ' 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.

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

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Code does not work      JamWis ... RobinRH   |   Edit   |   Show History

2 Errors: Name 'FavoritePictures' is not declared.

Resolved with following statement:

Dim

FavoritePictures AsString = _

My.Computer.FileSystem.SpecialDirectories.MyDocuments _

&

"\FavoritePictures.txt"

If there is different or better solution I would be grateful for advice - beginner!


It works if you complete the previous lesson as instructed in step 1.

I think that this would work better      gblilleyusmc   |   Edit   |   Show History

Hello, im a complete newbie here, so if there is something better let me know! Thanks.

By creating a new variable tied to favoritepictures.txt, wouldn't that simply create a file if there was none? This would cause a search to see if it exists to be pointless.

I have found that this works just as well without creating a new variable:

If

My.Computer.FileSystem_

.FileExists(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\favoritepictures.txt")

Then

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker