Checking whether the directory exists

Using a particular directory path, we can identify if any directory with that path exists in the filesystem or not. In the code implementation, we are creating a DirectoryInfo object and then using the Exists property to check whether the directory exists in the filesystem:

DirectoryInfo directoryInfoExists = new DirectoryInfo("C:\UCN Code Base\Programming-in-C-Exam-70-483-MCSD-Guide\Book70483Samples\Chapter 20");
if (directoryInfoExists.Exists)
{
Console.WriteLine("It exists");
}
else
{
Directory.CreateDirectory("Does not exist");
}

If we execute the code using the path mentioned during the creation of the DirectoryInfo object, the program execution will determine whether there is a directory in the filesystem in the specified path. Thus, when the program is executed, as we do not currently have a sub-folder called Chapter 20 in the Book70483Samples base folder we will see Does not exists in the console output window. The following is globally the relevant  output on the console window:

In the next section, we will look at how to create a directory in a filesystem using C#.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.247.68