目录中.Exist:标识错误的类型
本文关键字:错误 类型 标识 Exist | 更新日期: 2023-09-27 18:10:42
我使用Directory.Exist
来测试一个目录,但是我不能区分错误的类型。
我想要区分该目录是否不存在或不允许访问。我看到了这个链接c#测试用户是否有对文件夹的写权限,但是它很长很复杂。没有更简单的方法吗?
可以使用Exist
方法检查目录是否存在。这样的
if(Directory.Exists(directory)) {
// directory exists
try
{
// and is accessible by user...
File.GetAccessControl(filePath);
return true;
}
catch (UnauthorizedAccessException)
{
// but is unable to be accessed...
return false;
}
} else {
// directory doesn't exist, so no file accessible.
}
这对你来说是一个简单易懂的代码。每个方法都有自己的注释