Bagikan melalui


FileInfo.Exists Properti

Definisi

Mendapatkan nilai yang menunjukkan apakah ada file.

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

Nilai Properti

true jika file ada; false jika file tidak ada atau jika file adalah direktori.

Contoh

Contoh kode berikut menggunakan Exists properti pastikan ada file sebelum membukanya. Anda dapat menggunakan teknik ini untuk melemparkan pengecualian kustom ketika file tidak ditemukan.

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

Keterangan

Ketika pertama kali dipanggil, FileInfo panggilan Refresh dan cache informasi tentang file. Pada panggilan berikutnya, Anda harus memanggil Refresh untuk mendapatkan salinan informasi terbaru.

Properti Exists mengembalikan false jika terjadi kesalahan saat mencoba menentukan apakah file yang ditentukan ada. Ini dapat terjadi dalam situasi yang menimbulkan pengecualian seperti meneruskan nama file dengan karakter yang tidak valid atau terlalu banyak karakter, disk yang gagal atau hilang, atau jika pemanggil tidak memiliki izin untuk membaca file.

Berlaku untuk

Lihat juga