File.Exists(String) 方法


判斷指定的檔案是否存在。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



要檢查的檔案。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 方法並對檔案執行另一項作業(例如 Delete)時,另一個進程可能會在檔案之間執行某個動作。Be 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 方法會傳回 falseThe 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