.NET 4.x Проверка прав доступа к папке - C#
Формулировка задачи:
Прошу помощи. Необходимо в указаном пути перебрать папки на доступ к ней юзера.
А вот как сравнить, что пользователь имеет доступ на запись в директорию.
Листинг программы
- string abc;
- Console.WriteLine("Введите диска");
- abc = Console.ReadLine();
- string drive = abc + @":\";
- DirectoryInfo dir = new DirectoryInfo(drive); //тут я получил директорию где лежат папки
- DirectoryInfo[] alldir = dir.GetDirectories();//получил все директории на диске
- string nameuser = WindowsIdentity.GetCurrent().Name;
- FileSystemAccessRule acr = new FileSystemAccessRule(nameuser, FileSystemRights.Write, AccessControlType.Allow);
- foreach (DirectoryInfo name in alldir) //перебираем все папки в директории
- {
- DirectorySecurity ds = name.GetAccessControl(AccessControlSections.Access);//получаем права папок
Решение задачи: «.NET 4.x Проверка прав доступа к папке»
textual
Листинг программы
- string drive = abc + @":\";
- DirectoryInfo dir = new DirectoryInfo(drive);
- DirectoryInfo[] alldir = dir.GetDirectories();
- WindowsIdentity wi = WindowsIdentity.GetCurrent();
- foreach (DirectoryInfo name in alldir)
- {
- DirectorySecurity ds = name.GetAccessControl(AccessControlSections.Access);
- AuthorizationRuleCollection rules = ds.GetAccessRules(true, true, typeof(SecurityIdentifier));
- foreach (FileSystemAccessRule rl in rules)
- {
- SecurityIdentifier sid = (SecurityIdentifier)rl.IdentityReference;
- if (((rl.FileSystemRights & FileSystemRights.WriteData) == FileSystemRights.WriteData))
- {
- if ((sid.IsAccountSid() && wi.User == sid) ||
- (!sid.IsAccountSid() && wi.Groups.Contains(sid)))
- {
- if (rl.AccessControlType == AccessControlType.Allow)
- {
- Console.WriteLine("Доступ к папке {0} для пользователя - {1} разрешен", name.Name, wi.Name);
- }
- else
- {
- Console.WriteLine("Доступ к папке {0} для пользователя - {1} запрещен", name.Name, wi.Name);
- }
- }
- }
- }
- }
- Console.ReadLine();
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д