【发布时间】:2020-06-27 09:54:42
【问题描述】:
我正在做一个项目,我必须对文件执行完整性检查。我需要确保当前系统用户对文件具有读取权限,我首先尝试使用:
var readPermission = new FileIOPermission(FileIOPermissionAccess.Read, filePath);
try
{
readPermission.Demand();
}
catch (SecurityException ex)
{
//handle the exception, which should be thrown if current user does NOT have the read permission
}
那没有用,例如没有抛出异常,所以我尝试这样做:
var readPermission = new FileIOPermission(FileIOPermissionAccess.Read, filePath);
if(! SecurityManager.IsGranted(readPermission))
{
throw new SecurityException(
String.Format("System user: {0} does not have read access to file {1}", User.Identity.Name, filePath)
);
}
然而,SecurityManager api 似乎大部分已被弃用,所以这似乎也是一个死胡同。有没有其他方法可以告诉用户对文件有什么权限?
【问题讨论】:
-
你的意思是它不起作用 - 没有抛出异常但你无权访问?
-
@RicardoPeres 完全正确。
-
@TomTom 询问如何检查文件是否可读并不是题外话......
-
可能重复 stackoverflow.com/questions/21623343 :您不能依赖此方法的结果。似乎除了尝试以所需的访问权限打开文件之外别无他法。
-
@Clint 不幸的是,由于整个电晕情况,我会尽快(在接下来的 24 小时内),我的工作日非常忙碌,atm。不过,我会在尝试后立即通知您,非常感谢您
标签: c# .net-core file-permissions .net-core-3.1