Let’s say we need to find the following file −
E:\new.txt
To check the existence of the above file, use the Exists() method −
if (File.Exists(@"E:\new.txt")) {
Console.WriteLine("File exists...");
}Here is the complete code to check the existence of a file −
Example
using System;
using System.IO;
public class Demo {
public static void Main() {
if (File.Exists(@"E:\new.txt")) {
Console.WriteLine("File exists...");
} else {
Console.WriteLine("File does not exist in the E directory!");
}
}
}Output
File does not exist in the E directory!