Now for lets first create a new folder and add a file in it. Please check the below file location.
string folderPath = "D:\\DemoProject\\DemoFolder\\";
string fileName = "codemantra99.txt";
string finalPath = string.Format("{0}{1}", folderPath, fileName);
Console.WriteLine("Folder Path: "+folderPath);
Console.WriteLine("File Name: " + fileName);
if (File.Exists(finalPath))
{
Console.WriteLine(string.Format("File '{0}' exists.", fileName));
}
else
{
Console.WriteLine(string.Format("File '{0}' does not exists. ", fileName));
}
In above code I have defined two different variables first for the folder path and second one is for the file name. Now I have used string.Format(format, object) to concatenate or combine the string variables to get the complete path of the file. After that I have used File.Exists(Path) to validate file exists. now let's run the code to check the output.