How to: Determine if a Directory Exists in Visual Basic

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

The call to DirectoryExists requires FileIOPermission.

This method throws no exceptions.

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

Walkthrough: Manipulating Files and Directories in Visual Basic

Reference

My.Computer.FileSystem.DirectoryExists Method

Other Resources

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