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

优质内容筛选与推荐>>
1、SQL查看小数位数大于两位的
2、java 异常java.lang.UnsupportedOperationException
3、mysql优化20条原则
4、datatable 的ajax修改参数,post可以传参处理
5、global.fun.php


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号