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

注釈

最初に呼び出されると、 が呼び出Refreshされ、FileInfoファイルに関する情報がキャッシュされます。 以降の呼び出しでは、 を呼び出 Refresh して、情報の最新のコピーを取得する必要があります。

プロパティは Existsfalse 指定されたファイルが存在するかどうかを判断しようとしたときにエラーが発生した場合に を返します。 これは、無効な文字または文字数が多すぎるファイル名の渡し、ディスクの障害または不足、または呼び出し元がファイルを読み取るアクセス許可を持っていない場合など、例外が発生する場合に発生する可能性があります。

適用対象

こちらもご覧ください