关于对文件路径权限判断的记录

首先需要添加引用

using System.Security.AccessControl;

以下为具体代码,其中fileServerPath为需要判断的文件路径

 #region Authority judgment
            DirectorySecurity fileAcl = Directory.GetAccessControl(fileServerPath);
            var rules = fileAcl.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount)).OfType<FileSystemAccessRule>().ToList();
            var currentUser = Path.Combine(Environment.UserDomainName, Environment.UserName);
            bool permission = false;
            foreach (var rule in rules)
            {
                if (rule.IdentityReference.ToString() == currentUser) permission = true;
            }
            PublicMethod.OutputParameterWriteToLog(MethodBase.GetCurrentMethod().Name, string.Format("Current user [{0}] [{1}] access to file path [{2}]", currentUser, (permission ? "have" : "does not have"), fileServerPath));
#endregion

你可能感兴趣的:(C#,c#,代码节选)