Share via


How to: Determine if a Directory Exists in Visual Basic

You can use the DirectoryExists method to determine whether or not a specified directory exists.

The call to DirectoryExists requires FileIOPermission.

This method throws no exceptions.

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 directory exists

  • Use the My.Computer.FileSystem.DirectoryExists method, supplying the path, to determine whether or not the specified directory exists. The following example checks to determine whether or not the directory C:\backup\logs exists and checks its properties.

    Dim logDirectoryProperties As System.IO.DirectoryInfo
    If My.Computer.FileSystem.DirectoryExists("C:\backup\logs") Then
        logDirectoryProperties = My.Computer.FileSystem.GetDirectoryInfo("C:\backup\logs")
    End If
    

See Also

Tasks

How to: Determine if a File Exists in Visual Basic

Troubleshooting: Reading from and Writing to Text Files (Visual Basic)

Walkthrough: Manipulating Files and Directories in Visual Basic

Reference

DirectoryExists

Other Resources

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