File.Exists(String) is an inbuilt File class method that is used to determine whether the specified file exists or not. This method returns true if the caller has the required permissions and path contains the name of an existing file; otherwise, false. Also, if the path is null, then this method returns false.
Syntax:
public static bool Exists (string path);
Here, path is the specified path that is to be checked.
Program 1: Before running the below code, a file file.txt is created with some contents shown below:

CSharp
using System;
using System.IO;
class GFG {
static void Main()
{
if (File.Exists( "file.txt" )) {
Console.WriteLine( "Specified file exists." );
}
else {
Console.WriteLine( "Specified file does not " +
"exist in the current directory." );
}
}
}
|
Output:
Specified file exists.
Program 2: Before running the below code, no file is created.
CSharp
using System;
using System.IO;
class GFG {
static void Main()
{
if (File.Exists( "file.txt" )) {
Console.WriteLine( "Specified file exists." );
}
else {
Console.WriteLine( "Specified file does not" +
" exist in the current directory." );
}
}
}
|
Output:
Specified file does not exist in the current directory.