【发布时间】:2011-10-29 11:50:14
【问题描述】:
我对文件系统和权限/权限访问(又名访问控制列表,ACL)相当陌生。在针对 ACL 进行编码时,我无法为文件设置我想要的属性。我不确定我对 FileSystemRights 成员的理解是否错误,或者我完全做错了事。 (我已经在这部分花费了相当长的时间)
我想做的是更改文件的权限,使其只能单独阅读,不能在其他地方编辑、重命名、删除和复制。
使用 MSDN 的示例,这是我目前所拥有的:
try
{
//Get current identity
WindowsIdentity self = System.Security.Principal.WindowsIdentity.GetCurrent();
// Add the access control entry to the file.
AddFileSecurity(filename, self.Name, FileSystemRights.Modify, AccessControlType.Deny);
AddFileSecurity(filename, self.Name, FileSystemRights.Write, AccessControlType.Deny);
AddFileSecurity(filename, self.Name, FileSystemRights.ReadAndExecute, AccessControlType.Allow);
// Remove the access control entry from the file.
RemoveFileSecurity(filename, self.Name, FileSystemRights.ReadAndExecute, AccessControlType.Deny);
RemoveFileSecurity(filename, self.Name, FileSystemRights.Read, AccessControlType.Deny);
Console.WriteLine("Done.");
}
catch (Exception e)
{
Console.WriteLine(e);
}
我的逻辑是:
- 添加拒绝修改权限(拒绝 .Modify 会导致文件不可读)
- 添加拒绝写入权限
- 添加允许 ReadAndExecute 权限
- 删除 ReadAndExecute 上的 Deny 条目(作为 .Modify 拒绝 ReadAndExecute)
- 删除读取时拒绝条目(作为 .Modify 拒绝读取)
我做这部分正确吗?如果没有,请告知我应该怎么做才能使文件仅可读且不可编辑、可重命名、可删除和可复制。非常感谢!
【问题讨论】:
-
只要文件可读,它就永远是可复制的!
-
是否有可能(任何一种解决方法)使其可读但防止它被复制到其他地方?
-
否 - 任何可读的都是可复制的...
-
假设我只是将文件属性设置为“只读”,那么该文件是否可以在其他计算机上复制、编辑和删除?
-
它是可复制的,只要它是可读的......反过来,一切都可以完成(编辑,删除......)
标签: c# filesystems console-application rights