c#获取文件权限
代码
百度没有找到获取文件权限的方法,第一次写c#:
using System;
using System.IO;
using System.Security.AccessControl;
using System.Security.Principal;
class Program
{
static void Main(string[] args)
{
FileInfo file = new FileInfo("d:\\mystore");
AuthorizationRuleCollection accessRules = file.GetAccessControl().GetAccessRules(true, true,
typeof(System.Security.Principal.SecurityIdentifier));
foreach (FileSystemAccessRule rule in accessRules)
{
Console.Write(rule.IdentityReference.Translate(typeof(NTAccount)) + " ");
Console.Write(rule.FileSystemRights);
Console.WriteLine(" "+rule.AccessControlType);
}
Console.Read();
}
}
输出:
用户 | 权限 | 控制类型 |
---|---|---|
BUILTIN\Administrators | FullControl | Allow |
NT AUTHORITY\SYSTEM | FullControl | Allow |
NT AUTHORITY\Authenticated Users | Modify, Synchronize | Allow |
BUILTIN\Users | FullControl | Allow |