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

定義

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

public:
 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

參數

path
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."))

備註

方法不應用於路徑驗證, 這個方法只會檢查中path指定的檔案是否存在。 ExistsThe Exists method should not be used for path validation, this method merely checks if the file specified in path exists. 傳遞不正確路徑以Exists false傳回。Passing 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. 如需可接受路徑的範例File, 請參閱。For examples of acceptable paths, see File.

若要檢查目錄是否存在, 請Directory.Exists參閱。To 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. 若要取得目前的工作目錄, GetCurrentDirectory請參閱。To obtain the current working directory, see GetCurrentDirectory.

如果path描述目錄, 這個false方法會傳回。If 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.

安全性

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

適用於

另請參閱