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,無論路徑是否存在;方法不會拋出例外。

適用於