FileSystem.FileExists(String) 方法

定義

如果指定的檔案存在,則傳回 True

public:
 static bool FileExists(System::String ^ file);
public static bool FileExists (string file);
static member FileExists : string -> bool
Public Shared Function FileExists (file As String) As Boolean

參數

file
String

檔案的名稱和路徑。

傳回

如果檔案存在,則傳回 True,否則這個方法會傳回 False。 如果參數代表目錄的路徑,而非檔案,則此方法也會傳回 False

例外狀況

檔名結尾為反斜杠 (\) 。

範例

此範例會檢查檔案 Check.txt 是否存在,並在消息框中提供資訊。

If My.Computer.FileSystem.FileExists("c:\Check.txt") Then
    MsgBox("File found.")
Else
    MsgBox("File not found.")
End If

備註

如果應用程式沒有足夠的許可權可讀取指定的檔案, FileExists 則不論路徑是否存在,方法都會傳回 False;此方法不會擲回例外狀況。

適用於