[ Team LiB ] |
Recipe 11.9 Determining Whether a Directory ExistsProblemYou need to determine whether a directory exists prior to creating or performing an action on that directory. SolutionUse 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 } DiscussionDetermining 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. This method returns a bool indicating if the directory was found (true) or not (false). See AlsoSee the "Directory Class" topic in the MSDN documentation. |
[ Team LiB ] |