Recipe12.9.Determining Whether a Directory Exists


Recipe 12.9. Determining Whether a Directory Exists

Problem

You need to determine whether a directory exists prior to creating or performing an action on that directory.

Solution

Use the static Exists method on the Directory class to determine whether a directory currently exists:

 if (Directory.Exists(@"c:\delete\test")) {     // Operate on that directory here } 

Discussion

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.

This method returns a bool indicating whether the directory was found (true) or not (false).

See Also

See the "Directory Class" topic in the MSDN documentation.



C# Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2004
Pages: 424

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net