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:
// C# program to illustrate the usage // of File.Exists(String) method // Using System and System.IO namespaces using System; using System.IO; class GFG { static void Main() { // Checking the existance of the specified 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.
// C# program to illustrate the usage // of File.Exists(String) method // Using System and System.IO namespaces using System; using System.IO; class GFG { static void Main() { // Checking the existance of the specified 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.
Recommended Posts:
- Array.GetValue() Method in C# with Examples | Set - 1
- File.GetLastWriteTimeUtc() Method in C# with Examples
- MathF.Sin() Method in C# with Examples
- Double.CompareTo Method in C# with Examples
- UInt16.GetHashCode Method in C# with Examples
- Int64.CompareTo Method in C# with Examples
- MathF.Truncate() Method in C# with Examples
- MathF.Exp() Method in C# with Examples
- C# | Queue<T>.TrimExcess Method with Examples
- C# | Stack<T>.TrimExcess Method with Examples
- Array.BinarySearch(Array, Object) Method with examples in C#
- C# | Char.GetHashCode() Method with Examples
- C# | Char.GetTypeCode() Method with Examples
- C# | Char.GetUnicodeCategory(String, Int32) Method with Examples
- C# | Object.GetHashCode() Method with Examples
- C# | Object.GetTypeCode() Method with Examples
- Array.BinarySearch(Array, Int32, Int32, Object) Method with examples in C#
- Stack.ToString() Method in C# with examples
- Graphics.Clear() Method in C# with Examples
- Type.GetConstructors() Method in C# with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

