我有一个检查权限级别的EventHandler。
private void button1_Click(object sender, EventArgs e)
{
int id = 1;
XMLPermSheet.CreateXML();
string directory = textBox1.Text;
DirectoryInfo di = new DirectoryInfo(directory);
DirectoryInfo[] sdi = di.GetDirectories();
foreach (DirectoryInfo tdi in sdi)
{
if (!tdi.ToString().EndsWith("System Volume Information") && !tdi.ToString().Contains("$RECYCLE.BIN"))
{
XMLPermSheet.AddPath(tdi.ToString(), id);
DirectorySecurity ds = tdi.GetAccessControl();
foreach (AccessRule rule in ds.GetAccessRules(true, true, typeof(NTAccount)))
{
richTextBox1.AppendText(string.Format("{0} || Identity = {1}; Access = {2} \r\n", tdi.ToString(),
rule.IdentityReference.Value, rule.AccessControlType));
XMLPermSheet.AddIdentity(rule.IdentityReference.Value.ToString(), rule.AccessControlType.ToString(), tdi.ToString());
}
id += 1;
}
}
}
rule.AccessControlType.ToString()仅返回“允许”或“拒绝”,但我需要得到一些内容:
观点:真实 添加:真实 修改:假 删除:错误
我怎样才能实现这个目标?
答案 0 :(得分:2)
您必须将foreach中的 AccessRule 更改为 FileSystemAccessRule 。然后,您可以访问属性FileSystemRights。这个answer解释了如何获得权利。
简短版本,关于如何检查用户或组是否具有权限:
//Example: Change
bool hasChangePermission = rule.FileSystemRights.HasFlag(FileSystemRights.ChangePermissions);
//Example: Write
bool hasWritePermission = rule.FileSystemRights.HasFlag(FileSystemRights.Write);
这是一个小例子方法:
public string GetRuleAsString(FileSystemAccessRule rule)
{
string userName = rule.IdentityReference.Value;
//Example: Change
bool hasChangePermission = rule.FileSystemRights.HasFlag(FileSystemRights.ChangePermissions);
//Example: Write
bool hasWritePermission = rule.FileSystemRights.HasFlag(FileSystemRights.Write);
return String.Format("{0}\n Change: {1}\n Write: {2}", userName, hasChangePermission, hasWritePermission);
}