File.Exists(String) File.Exists(String) File.Exists(String) File.Exists(String) Method


判斷指定的檔案是否存在。Determines whether the specified file exists.

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


String String String String

要檢查的檔案。The file to check.


如果呼叫端具有必要的權限,而且 true 包含現有檔案的名稱,則為 path,否則為 falsetrue if the caller has the required permissions and path contains the name of an existing file; otherwise, false. 如果 falsepath、無效路徑或長度為零的字串,這個方法也會傳回 nullThis method also returns false if path is null, an invalid path, or a zero-length string. 如果呼叫端沒有充足權限讀取指定檔案,沒有例外狀況會擲回,並且不論 false 是否存在,方法都會傳回 pathIf the caller does not have sufficient permissions to read the specified file, no exception is thrown and the method returns false regardless of the existence of path.


下列範例會判斷檔案是否存在。The following example determines if a file exists.

string curFile = @"c:\temp\test.txt";
Console.WriteLine(File.Exists(curFile) ? "File exists." : "File does not exist.");
Dim curFile As String = "c:\temp\test.txt"
Console.WriteLine(If(File.Exists(curFile), "File exists.", "File does not exist."))


Exists方法不應該用於路徑驗證,這個方法只會檢查中是否指定的檔案path存在。The Exists method should not be used for path validation, this method merely checks if the file specified in path exists. 傳遞至無效的路徑Exists傳回falsePassing an invalid path to Exists returns false. 若要檢查路徑是否包含任何無效的字元,您可以呼叫GetInvalidPathChars方法來擷取檔案系統無效字元。To check whether the path contains any invalid characters, you can call the GetInvalidPathChars method to retrieve the characters that are invalid for the file system. 您也可以建立規則運算式來測試是否為您的環境的路徑無效。You can also create a regular expression to test the whether the path is valid for your environment. 如需可接受的路徑的範例,請參閱FileFor examples of acceptable paths, see File.

若要檢查目錄是否存在,請參閱Directory.ExistsTo check if a directory exists, see Directory.Exists.

請注意,另一個處理序可以可能處理的項目檔案在您呼叫的時間之間Exists方法和另一個對檔案執行作業,例如DeleteBe aware that another process can potentially do something with the file in between the time you call the Exists method and perform another operation on the file, such as Delete.

path參數可以指定相對或絕對路徑資訊。The path parameter is permitted to specify relative or absolute path information. 相對於目前工作目錄,會解譯相對路徑的資訊。Relative path information is interpreted as relative to the current working directory. 若要取得目前工作目錄,請參閱GetCurrentDirectoryTo obtain the current working directory, see GetCurrentDirectory.

如果path描述的目錄中,這個方法會傳回falseIf path describes a directory, this method returns false. 從已移除尾端空格path參數,然後再判斷檔案是否存在。Trailing spaces are removed from the path parameter before determining if the file exists.

Exists方法會傳回false如果嘗試判斷指定的檔案是否存在時發生任何錯誤。The Exists method returns false if any error occurs while trying to determine if the specified file exists. 可能發生這種情況下,會引發例外狀況,例如傳遞檔名無效的字元或太多字元、 失敗或遺失的磁碟,或如果呼叫端沒有讀取檔案的權限。This can occur in situations that raise exceptions such as passing a file name with invalid characters or too many characters, a failing or missing disk, or if the caller does not have permission to read the file.


從指定的檔案讀取。for reading from the specified file. 相關聯的列舉型別: ReadAssociated enumeration: Read