Udostępnij za pośrednictwem


How to: Determine if a File Exists in Visual Basic

You can use the 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

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. 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 (Visual Basic)

Walkthrough: Manipulating Files and Directories in Visual Basic

Reference

FileExists

Other Resources

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