This topic has not yet been rated - Rate this topic

FileInfo.Exists Property

Updated: June 2010

Gets a value indicating whether a file exists.

Namespace:  System.IO
Assembly:  mscorlib (in mscorlib.dll)
public override bool Exists { get; }

Property Value

Type: System.Boolean
true if the file exists; false if the file does not exist or if the file is a directory.

When first called, FileInfo calls Refresh and caches information about the file. On subsequent calls, you must call Refresh to get the latest copy of the information.

The Exists property returns false if any error occurs while trying to determine if the specified file exists. This can occur in situations that raise exceptions such as passing a file name with invalid characters or too many characters, a failing or missing disk, or if the caller does not have permission to read the file.

The following code example uses the Exists property ensure a file exists before opening it. You can use this technique to throw a custom exception when the file is not found.


public byte[] OpenDataFile(string FileName)
{
    // Check the FileName argument.
    if (FileName == null || FileName.Length == 0)
    {
        throw new ArgumentNullException("FileName");
    }

    // Check to see if the file exists.
    FileInfo fInfo = new FileInfo(FileName);

    // You can throw a personalized exception if 
    // the file does not exist.
    if (!fInfo.Exists)
    {
        throw new FileNotFoundException("The file was not found.", FileName);
    }

    // Open the file.
    FileStream fStream = new FileStream(FileName, FileMode.Open);

    // Create a buffer.
    byte [] buffer = new byte[fStream.Length];

    // Read the file contents to the buffer.
    fStream.Read(buffer, 0, (int)fStream.Length);

    // return the buffer.
    return buffer;

}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Date

History

Reason

June 2010

Described errors that cause the property to return false.

Information enhancement.

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Race condition
The way this is built is sensitive to racing. Not that this actual application is, but this code should not be encouraged. The correct code would be aquired by replacing "FileStream fStream = new FileStream(FileName, FileMode.Open);" with "FileStream fStream = fInfo.Open(FileMode.Open);". Otherwise, the file at location FileName could be switched between the check and the open. As already said, not dangerous here, but should not be encouraged behavior.
Fails because?
It should throw! If that contravenes MS strategy, it should not be a property so that it can throw.