dotnet / dotnet/dotnet-api-docs

Documentation for FileInfo.Exists and File.Exists is misleading

Open
#3,277 5 comments 0 reactions 0 assignees View on GitHub
area-System.IO doc-enhancement help wanted Pri3
Dominant language
C#
Stars
949
Forks
1.7k
Avg merge
3d 27m
Merged PRs (30d)
49

Description

Documentation for FileInfo.Exists and File.Exists is misleading

File.Exists "returns" currently reads:

true if the caller has the required permissions and path contains the name of an existing file; otherwise, false. This method also returns false if path is null, an invalid path, or a zero-length string. If the caller does not have sufficient permissions to read the specified file, no exception is thrown and the method returns false regardless of the existence of path.

Should read:

true if the caller has the required permissions and path contains the name of an existing file; otherwise, false. This method also returns false on an invalid filename or if any error prevents determining if the file exists or not.

FileSystemInfo.Exists "returns" currently reads:

true if the file or directory exists; otherwise, false.

Should read:

true if the file or directory is known to exist; otherwise, false. This method returns false if an error prevented determining if the file exists or not. If FileSystemInfo.Exists returns false, call FileSystemInfo.tLastWriteTimeUTC to throw the exception for the error. If LastWriteTimeUTC returns at all, the file or directory is known to not exist.

Remakrs on FileSystemInfo.Exists should contain this blob:

FileSystemInfo reports on existence based on a snapshot of the file in question that may be invalid by the time FileSystemInfo.Exists() returns. Therefore, the following code can still throw FileNotFoundException: var fileinfo = new FileInfo(pathstring); if (fileinfo.Exists()) using (var f = new FileStream(fileinfo.FullName, FileMode.Open) { /* ... */ } else {var ignoredvalue = fileInfo.LastWriteTimeUTC; }

General Commentary (why these specific changes):

Not checking if the result of FileInfo.Exists is trustworthy is almost always a bug, and is always a bug in batch processing (and therefore all library modules). File.Exists() has no use for the reason of there being no way to check if false was a successful false or an error. Just calling fileInfo.LastWriteTimeUTC != new DateTime(1601, 1, 1, 0, 0, 0) as FileInfo.Exists won't work because that's a valid time that somebody could have set a file to. (And I don't trust that value's right on Unix anyway but I digress.)

Most of the time, FileInfo isn't what you want anyway. The static file provider in MVC has some bizarre corner case bugs that would not exist had it checked for a file's existence by opening it and catching FileNotFoundException.

Original Discussion: https://github.com/dotnet/corefx/issues/26893

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.