C#中當程序的訪問權限不足時,Directory.Exists和File.Exists方法不會拋出異常報錯


有些時候,我們開發的C#應用程序的執行賬號,可能沒有對一些文件夾和文件的訪問權限,當我們使用Directory.Exists和File.Exists方法去判斷這些文件夾和文件是否存在的時候,Directory.Exists和File.Exists方法並不會拋出異常報錯,這兩個方法會返回false,表示查找的文件夾和文件不存在。盡管文件夾和文件實際上是存在的,只是C#程序的執行賬號沒有權限訪問而已,但是Directory.Exists和File.Exists方法還是會返回false,並不會拋出異常報錯。

 

以下是MSDN對Directory.Exists和File.Exists方法的解釋,其中也提到了權限不足的問題:

Directory.Exists

Returns
true if path refers to an existing directory; false if the directory does not exist or an error occurs when trying to determine if the specified directory exists.

 

File.Exists

Returns
true if the caller has the required permissions and path contains the name of an existing file; otherwise, false. This method also returns false if path is null, an invalid path, or a zero-length string. If 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.

 

 

參考鏈接

Directory.Exists
File.Exists

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM