string filePath = "C:\\example\\example.txt"; if(File.Exists(filePath)) { Console.WriteLine("The file exists."); } else { Console.WriteLine("The file does not exist."); }
string directoryPath = "C:\\example"; if(Directory.Exists(directoryPath)) { Console.WriteLine("The directory exists."); } else { Console.WriteLine("The directory does not exist."); }Package Library: The FilePath Exists method is included in the System.IO namespace, which is part of the .NET Framework. Therefore, no additional packages or libraries are necessary to use this method.