Checking whether a file exists

This basically involves checking whether a file with the given path exists or not. This can help us write fail-safe code in such a way that we read a file only after establishing that it exists in the given path:

string file = "C:\UCN Code Base\Programming-in-C-Exam-70-483-MCSD-Guide\Book70483Samples\Chapter 20\IO Operations.txt";
if(File.Exists(file))
{
Console.WriteLine("File Exists in the mentioned path");
}
else
{
Console.WriteLine("File does not exists in the mentioned path");
}

File is a static class available in the System.IO namespace. This class provides operations that we can use to execute functionalities related to file access. In the preceding code, we have declared a file path and, using the static File class, we are checking whether the file indeed exists in the given path. 

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

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