5.14. Determine Whether a Path Is a Directory or a File

Problem

You have a path (in the form of a string), and you want to determine whether it corresponds to a directory or a file.

Solution

Test the path with the Directory.Exists and File.Exists methods.

How It Works

The System.IO.Directory and System.IO.File classes both provide a Shared Exists method. The Directory.Exists method returns True if a supplied relative or absolute path corresponds to an existing directory, even a shared folder with an UNC name. File.Exists returns True if the path corresponds to an existing file.

As an alternative, you can use the Shared FileExists and DirectoryExists methods of the My.Computer.FileSystem class. These methods work in the same way as the Exists ...

Get Visual Basic 2008 Recipes: A Problem-Solution Approach now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.