std::filesystem:: exists
Checks if the given file status or path corresponds to an existing file or directory.
Contents
[edit] Parameters
| s | — | file status to check |
| p | — | path to examine |
| ec | — | out-parameter for error reporting in the non-throwing overload |
[edit] Return value
true if the given path or file status corresponds to an existing file or directory, false otherwise.
[edit] Exceptions
No filesystem exception is thrown if object does not exist (use return value).
[edit] Notes
The information provided by this function is usually also provided as a byproduct of directory iteration. During directory iteration, calling exists ( * iterator ) is less efficient than exists ( iterator — > status ( ) ) .
What is difference between File.Exists("") and FileInfo exists
I have an *.exe file in \ProgramFiles(x86)\MyAppFolder.
In x86 application I check if the file exists (64 bit system). simple:
The result is: «fileExists == false» (the file is really there). It’s Virtualization as I understand.That issue described here Its ok. But next code:
Why is the result different in 1st and 2nd cases?
6 Answers 6
This is about the only difference and it has more to do with the nature of FileInfo :
So as you can see the value of fileInfo.Exists is cached the first time you use it.
Other than that, they do the same thing behind the scenes.
![]()
There is no difference, these methods use the exact same internal helper method inside the .NET Framework. Something you can see with a decompiler or the Reference Source source code, the helper method name is File.FillAttributeInfo().
Having duplication like this in the .NET Framework is pretty unusual, not exactly a Good Thing to have more than one way to accomplish the same thing. The File class is however special, it got added after a usability study conducted when .NET 1.0 shipped. The test subjects just had the basic BCL classes to work with, like FileStream and FileInfo, and otherwise only had MSDN documentation available. The test results were not very good, the File class got added to help programmers fall in the pit of success writing very basic file manipulation code. Like File.Exists() and File.ReadAllLines().
So it doesn’t have anything to do with the classes, you are just using them wrong. Like not actually using the same path. Do go easy on the forward slashes, the mapping to backward slashes happens inside Windows and is inconsistently implemented in other code. Using // certainly doesn’t do what you hope it does.
How to check if a file exists in C#
The File class in the System.IO namespace provides the Exists() method that checks the existence of a file. This method takes the path of the file as a string input and return true if the file exists at the specified path on the disk; otherwise, it returns false.
Syntax
The Exists() method will return false if:
- The input file does not exist.
- The input path is null .
- The input path is an empty string.
- The input path is invalid.
- The caller does not have sufficient permissions.
- The input path is a directory.
Code example
In the below example, the file test.txt is existing in the current directory.
The File.Exists() method returns true for this path and the program prints File test.txt exists .
Note: This method should not be used for validating a path since it only checks the existence of the file. Therefore, it does not throw an exception for invalid paths or any other error scenarios.
C# File.Exists
By
Priya Pedamkar

Introduction to C# File.Exists
In C#, File.Exists() method comes under System.IO namespace. It is used to check whether a file exists at the specified location or not. The following are some important points regarding File.Exists() method in C#:
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
- This method takes a string (path of the file) as input.
- It returns a Boolean value; returns true if the user has required permission to read the file and the file exists at the specified location else returns false.
- It also returns false if the path is null or if it is not valid or if its length is zero.
Syntax:
Web development, programming languages, Software testing & others
The syntax for File.Exists() method in C# is as follows:
In the above syntax, we have used File.Exists() method with an ‘if’ statement. Here, File is a class that contains the Exists() method. The File.Exists() method takes ‘file_path’ which is the path of the file as input to check if the file exists or not.
If the file exists then the corresponding user code will be executed.
How File.Exists() Method Works in C#?
To work with File.Exists() method, we first need to import System.IO namespace in our code. This method takes a string as an input which is the path of the file to be checked for existence. This method returns true if the file exists at the specified path and if the user has permission to read the file. If the file exists but the user does not have required permission to read the file then an exception will not be thrown but the method will return false. Apart from this, the method returns false if the input string (path of the file) is null or if its length is zero. The user should check that the path of the file is valid such as it is not empty, it does not only contain white spaces, it does not contain any invalid characters, the path of the file or file name is not too long, etc. If the path of the file is not valid then the method will return false.
We can check for the validity of the path by calling Path.GetInvalidPathChars() method which returns an array of characters that are not allowed in the pathname. Along with this, we can also create a regex (regular expression) to check for the validity of the path. While a user calls File.Exists() method on a file it is possible that at the same time another operation can be performed on the same file such as Delete operation. Thus, a user needs to be aware of this possibility. To File.Exists() method, the user is allowed to pass absolute or relative path information as input. The path specified as a relative path will be interpreted as relative to the current working directory. File.Exists() method works only for a file. If a user passes the path of the directory to the method then it will return false. The method removes trailing spaces if any from the path of the file before checking for the existence of the file.
Examples of File.Exists in C#
Example to check if the file exists in the current directory or not. If it exists the code will open the file to read its content else it will show a message that file does not exist in this directory.
Example #1
Code:
Output:

Example #2
Example to check if a file exists at the specified directory by writing the file path in two different ways as shown below: