How to: Determine if a File Exists in Visual Basic

You can use the My.Computer.FileSystem.FileExists Method to determine whether or not a specified file exists.

If the application does not have sufficient permissions to read the specified file, the FileExists method returns False regardless of the existence of path; the method does not throw an exception.

Note

  The options available in dialog boxes, and the names and locations of menu commands you see, might differ from what is described in Help, depending on your active settings or edition. This Help page was written with General Development Settings in mind. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.

Procedure

To determine if a file exists

  • Use the My.Computer.FileSystem.FileExists method, supplying the path, to determine whether or not the specified file exists. The following example checks to see whether or not the file Check.txt exists and supplies the information in a message box.

    If My.Computer.FileSystem.FileExists("c://Check.txt") Then
        MsgBox("File found.")
    Else
        MsgBox("File not found.")
    End If
    

See Also

Tasks

How to: Determine if a Directory Exists in Visual Basic

Troubleshooting: Reading from and Writing to Text Files

Walkthrough: Manipulating Files and Directories in Visual Basic

Reference

My.Computer.FileSystem.FileExists Method

Other Resources

Creating, Deleting, and Moving Files and Directories in Visual Basic