FileInfo.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;如果文件不存在或文件即是目录,则为 false

示例

下面的代码示例使用 属性, Exists 确保在打开文件之前文件存在。 可以使用此方法在找不到文件时引发自定义异常。

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

注解

首次调用时, FileInfo 调用 Refresh 并缓存有关文件的信息。 在后续调用中,必须调用 Refresh 以获取信息的最新副本。

如果尝试确定指定的文件是否存在时发生任何错误,则 Exists 属性将返回 false 。 在引发异常的情况下,可能会发生这种情况,例如传递包含无效字符或太多字符的文件名、磁盘失败或缺失,或者调用方没有读取文件的权限。

适用于

另请参阅