To determine whether a directory/folder exists prior to creating or performing an action on that directory, you can use the static Exists method of the Directory class. This method returns a bool indicating if the directory was found (true) or not (false).
Determining whether a directory exists can be critical to your code. If you try to delete a directory that no longer exists, a System.IO.DirectoryNotFoundException will be thrown. This can be handled by catching the exception and reporting the failure accordingly for your application.
The example of using static Exists as in the snippet code:
if (Directory.Exists(@"c:\test"))
{
// Operate on that directory here
}
Popularity: 1% [?]
RSS feed for comments on this post · TrackBack URI
Leave a reply