3

Создаю автоматический файловую структуру. И на папке Private, нужно отключить наследование. Но у меня не получается.

Вот мой код.

if (!Directory.Exists(path))
                        {
                            Directory.CreateDirectory(path);
                            SetFolderPermission(path, userName, FileSystemRights.ReadAndExecute);
                            SetFolderPermission(path, userName, FileSystemRights.Write);
                            RemoveFileSecurity(path, domainUsers, FileSystemRights.Write);
                            RemoveFileSecurity(path, domainUsers, FileSystemRights.Read);
                        }

И два метода

 public static void SetFolderPermission(string folderPath, string userName, FileSystemRights rights)
        {
            var directoryInfo = new DirectoryInfo(folderPath);
            var directorySecurity = directoryInfo.GetAccessControl();

            var fileSystemRule = new FileSystemAccessRule(userName,
                                                          rights,
                                                          InheritanceFlags.ObjectInherit |
                                                          InheritanceFlags.ContainerInherit,
                                                          PropagationFlags.None,
                                                          AccessControlType.Allow);

            directorySecurity.AddAccessRule(fileSystemRule);
            directoryInfo.SetAccessControl(directorySecurity);
        }

        public static void RemoveFileSecurity(string folderPath, string userName, FileSystemRights rights)
        {
            var directoryInfo = new DirectoryInfo(folderPath);
            var directorySecurity = directoryInfo.GetAccessControl();

            var fileSystemRule = new FileSystemAccessRule(userName,
                                                          rights,
                                                          InheritanceFlags.ContainerInherit |
                                                          InheritanceFlags.ContainerInherit ,
                                                          PropagationFlags.InheritOnly,
                                                          AccessControlType.Allow);

            directorySecurity.RemoveAccessRule(fileSystemRule);
            directoryInfo.SetAccessControl(directorySecurity);
        }

В первом методе я даю пользователю права на папку, а во втором пытаюсь отключить наследование для пользователей домена. И это скорее всего не правильно. Мне нужно поправить этот метод, чтобы он просто отключал бы наследование. Как мне это сделать?

1 ответ 1

5
directorySecurity.SetAccessRuleProtection(true, false);

О методе SetAccessRuleProtection

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.