Example #1
0
        //初始化界面
        private void InitDisplay(string fileName)
        {
            lblObjectName.Text = fileName;

            AuthorizationRuleCollection tempAccessRulesCollection = null;

            //如果是文件
            if (File.Exists(fileName))
            {
                FileInfo fileInfo = new FileInfo(fileName);
                tempAccessRulesCollection = fileInfo.GetAccessControl().GetAccessRules(true, true,
                                                                                       typeof(System.Security.Principal.SecurityIdentifier));
            }
            //如果是文件夹
            else if (Directory.Exists(fileName))
            {
                DirectoryInfo dirInfo = new DirectoryInfo(fileName);
                tempAccessRulesCollection = dirInfo.GetAccessControl().GetAccessRules(true, true,
                                                                                      typeof(System.Security.Principal.SecurityIdentifier));
            }


            AuthorizationRule[] tempAccessRulesArray = new AuthorizationRule[tempAccessRulesCollection.Count];
            tempAccessRulesCollection.CopyTo(tempAccessRulesArray, 0);

            //去重
            accessRulesArray = UniqAccessRules(tempAccessRulesArray);


            lvwGroupOrUserName.Items.Clear();

            //显示组或用户名列表
            for (int i = 0; i < accessRulesArray.Length; i++)
            {
                ListViewItem item = lvwGroupOrUserName.Items.Add(accessRulesArray[i].IdentityReference.Translate(typeof(NTAccount)).ToString());
                item.Tag        = i;
                item.ImageIndex = IconsIndexes.GroupOrUser;
            }

            //初始时默认当前选中的组或用户名为第一项
            lvwGroupOrUserName.HideSelection     = false;
            lvwGroupOrUserName.Items[0].Selected = true;

            //显示当前选中的组或用户名对该文件/文件夹具有的权限列表
            ShowPrivilegeList();
        }
        public static bool IsAuthorized(string path)
        {
            bool isAuthorized = false;

            try
            {
                var fileSecuirty = new FileSecurity(path, AccessControlSections.Access);
                AuthorizationRuleCollection acl = fileSecuirty.GetAccessRules(true, true, typeof(System.Security.Principal.SecurityIdentifier));
                AuthorizationRule[]         authorizationRuleCollection = new AuthorizationRule[acl.Count];
                acl.CopyTo(authorizationRuleCollection, 0);

                isAuthorized = authorizationRuleCollection.ToList().Where(rc =>
                {
                    var fileSystemAccessRule = (FileSystemAccessRule)rc;
                    return(fileSystemAccessRule.AccessControlType == AccessControlType.Allow && (fileSystemAccessRule.FileSystemRights & FileSystemRights.ListDirectory) == FileSystemRights.ListDirectory);
                }).Count() > 0;
            }
            catch (UnauthorizedAccessException)
            {
                Log(string.Format("Attempted to perform an unauthorized operation for {0}", path));
            }

            return(isAuthorized);
        }