Exemple #1
0
        /// <summary>
        /// Exporting permissions of directory
        /// </summary>
        /// <param name="directory">Папка права которой требуется вычислить</param>
        private void WalkDirectoryTree(string directory)
        {
            if (allow_work)
            {
                DirectoryInfo root = new DirectoryInfo(directory);
                if (root.Exists)
                {
                    AuthorizationRuleCollection ACLs_temp = root.GetAccessControl().GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount));
                    AuthorizationRuleCollection ACLs      = new AuthorizationRuleCollection();
                    foreach (FileSystemAccessRule ACL_temp in ACLs_temp)
                    {
                        if (checkBox2.Checked && !IsSystemUser(ACL_temp.IdentityReference.ToString()))
                        {
                            ACLs.AddRule(ACL_temp);//add only not system
                        }
                        else //not skip
                        {
                            ACLs.AddRule(ACL_temp);//add all
                        }
                    }
                    ACLs_temp = null;
                    if (ACLs.Count > 0)
                    {
                        foreach (FileSystemAccessRule ACL in ACLs)
                        {
                            string[] rights_Temp = ACL.FileSystemRights.ToString().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                            foreach (string right_Temp in rights_Temp)
                            {
                                string ACL_string = ACL.IdentityReference.ToString();
                                string ACL_IF     = ACL.InheritanceFlags.ToString();
                                string ACL_IsI    = ACL.IsInherited.ToString();
                                string right      = right_Temp.Trim();
                                switch (right_Temp)//Печатаем большими буквами чтобы визуально отличать, но при сортировке и фильтре чтобы не отличалось
                                {
                                case "268435456":
                                    right = "FULLCONTROLL";
                                    break;

                                case "-536805376":
                                    right = "EXECUTE";
                                    break;

                                case "-1073741824":
                                    right = "WRITE";
                                    break;

                                case "2147483648":
                                    right = "READ";
                                    break;

                                case "-1610612736":    //https://coderoad.ru/26427967/Get-acl-%D0%BF%D0%BE%D0%B2%D1%82%D0%BE%D1%80%D1%8F%D1%8E%D1%89%D0%B8%D0%B5%D1%81%D1%8F-%D0%B3%D1%80%D1%83%D0%BF%D0%BF%D1%8B-Powershell
                                    right = "READANDEXECUTE";

                                    if (ACL.AccessControlType.ToString().Equals("Allow"))
                                    {
                                        Invoke((ThreadStart) delegate { listView1.Items.Add(new ListViewItem(new string[] { directory, ACL_string, "SYNCHRONIZE", string.Empty, ACL_IF, ACL_IsI })); });
                                    }
                                    if (ACL.AccessControlType.ToString().Equals("Deny"))
                                    {
                                        Invoke((ThreadStart) delegate { listView1.Items.Add(new ListViewItem(new string[] { directory, ACL_string, string.Empty, "SYNCHRONIZE", ACL_IF, ACL_IsI })); });
                                    }
                                    break;

                                default:
                                    break;
                                }
                                try
                                {
                                    if (ACL.AccessControlType.ToString().Equals("Allow"))
                                    {
                                        Invoke((ThreadStart) delegate { listView1.Items.Add(new ListViewItem(new string[] { directory, ACL_string, right, string.Empty, ACL_IF, ACL_IsI })); });
                                    }
                                    if (ACL.AccessControlType.ToString().Equals("Deny"))
                                    {
                                        Invoke((ThreadStart) delegate { listView1.Items.Add(new ListViewItem(new string[] { directory, ACL_string, string.Empty, right, ACL_IF, ACL_IsI })); });
                                    }
                                }
                                catch (UnauthorizedAccessException ee) { MessageBox.Show(ee.StackTrace); }
                                catch (Exception ee) { MessageBox.Show(ee.StackTrace); }
                            }
                        }
                    }
                    try
                    {
                        List <DirectoryInfo> subDirs = new List <DirectoryInfo>(root.GetDirectories()); // рекурсией проверяем все подпапки
                        if (checkBox2.Checked == true)                                                  //remove system directories
                        {
                            foreach (string systemfolder in systemfolders)
                            {
                                foreach (DirectoryInfo subDir in subDirs)
                                {
                                    if (subDir.Name.ToString().Equals(systemfolder, StringComparison.CurrentCultureIgnoreCase))
                                    {
                                        subDirs.Remove(subDir);
                                        break;
                                    }
                                }
                            }
                            foreach (string systemfolder in systemfolders)
                            {
                                foreach (DirectoryInfo subDir in subDirs)
                                {
                                    if (subDir.Attributes.ToString().Contains("System"))
                                    {
                                        subDirs.Remove(subDir);
                                        break;
                                    }
                                }
                            }
                        }

                        List <string> Tree = new List <string>(root.FullName.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries));
                        if (Tree.Count < root_layer + Convert.ToInt32(textBox2.Text))
                        {
                            foreach (DirectoryInfo dirInfo in subDirs)
                            {
                                if (allow_work)
                                {
                                    WalkDirectoryTree(dirInfo.FullName);
                                }
                            }
                        }
                    }
                    catch { Invoke((ThreadStart) delegate { listView1.Items.Add(new ListViewItem(new string[] { directory, "Failed to get subfolders list. Acceess denied!?", string.Empty, string.Empty, string.Empty, string.Empty })); }); }
                }
                else
                {
                    MessageBox.Show("The folder does not exist. Canceling an operation.");
                }
            }
        }