FileInfo.Exists 属性

定义

获取指示文件是否存在的值。Gets a value indicating whether a file exists.

public:
 virtual property bool Exists { bool get(); };
public override bool Exists { get; }
member this.Exists : bool
Public Overrides ReadOnly Property Exists As Boolean

属性值

如果该文件存在,则为 true;如果文件不存在或文件即是目录,则为 falsetrue if the file exists; false if the file does not exist or if the file is a directory.

示例

下面的代码示例使用属性Exists , 在打开文件之前确保文件存在。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.

array<Byte>^ Openfile(String^ fileName)
{
    // Check the fileName argument.
    if (fileName == nullptr || fileName->Length == 0)
    {
        throw gcnew ArgumentNullException("fileName");
    }

    // Check to see if the file exists.
    FileInfo^ fInfo = gcnew FileInfo(fileName);

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

    try
    {
        // Open the file.
        FileStream^ fStream = gcnew FileStream(fileName, FileMode::Open);

        // Create a buffer.
        array<Byte>^ buffer = gcnew array<Byte>(fStream->Length);

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

        // return the buffer.
        return buffer;
    }
    catch (IOException^ ex)
    {
        Console::WriteLine(ex->Message);
        return nullptr;
    }
}
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;

}
Function OpenDataFile(ByVal FileName As String) As Byte()
    ' Check the FileName argument.
    If FileName Is Nothing OrElse FileName.Length = 0 Then
        Throw New ArgumentNullException("FileName")
    End If

    ' Check to see if the file exists.
    Dim fInfo As New FileInfo(FileName)

    ' You can throw a personalized exception if 
    ' the file does not exist.
    If Not fInfo.Exists Then
        Throw New FileNotFoundException("The file was not found.", FileName)
    End If

    ' Open the file.
    Dim fStream As New FileStream(FileName, FileMode.Open)

    ' Create a buffer.
    Dim buffer(fStream.Length) As Byte

    ' Read the file contents to the buffer.
    fStream.Read(buffer, 0, Fix(fStream.Length))

    ' return the buffer.
    Return buffer

End Function

注解

第一次调用FileInfoRefresh , 将调用并缓存有关文件的信息。When first called, FileInfo calls Refresh and caches information about the file. 在后续调用中, 必须调用Refresh以获取信息的最新副本。On subsequent calls, you must call Refresh to get the latest copy of the information.

如果Exists在尝试false确定指定文件是否存在时出现任何错误, 则属性将返回。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.

适用于

另请参阅