我试图拒绝或仅保留特定组中的用户删除共享文件夹中的文件和子文件夹的能力。每当我将高级权限设置为拒绝文件和子文件夹删除和/或将所有权限设置为活动时,除了删除文件和子文件夹的能力之外(我未选中这两个权限),我的用户将无法打开文件、对任何文件进行更改或以其他方式访问文件和子文件夹。我尝试过共享和ntfs权限设置的不同组合,我尝试过继承,删除继承,我尝试过一切,除了用锤子修复它。如果您有任何建议或需要更多信息来帮助理解我做错或没做的事情,请告诉我。
我不确定您是否正在寻找一个程序或自动化。无论如何,我将分享一个可能帮助您实现这一目标的脚本
using System;
using System.IO;
using System.Security.AccessControl;
class Program
{
static void Main()
{
string folderPath = @"C:\Path\To\Your\SharedFolder";
string username = "YourUsername";
DirectoryInfo directoryInfo = new DirectoryInfo(folderPath);
DirectorySecurity directorySecurity = directoryInfo.GetAccessControl();
FileSystemAccessRule allowRule = new FileSystemAccessRule(
username,
FileSystemRights.ReadAndExecute | FileSystemRights.ListDirectory | FileSystemRights.Read,
InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit,
PropagationFlags.None,
AccessControlType.Allow);
// Specify the access rule that denies delete permissions
FileSystemAccessRule denyRule = new FileSystemAccessRule(
username,
FileSystemRights.Delete | FileSystemRights.DeleteSubdirectoriesAndFiles,
InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit,
PropagationFlags.None,
AccessControlType.Deny);
directorySecurity.AddAccessRule(allowRule);
directorySecurity.AddAccessRule(denyRule);
directoryInfo.SetAccessControl(directorySecurity);
Console.WriteLine("Permissions defined successfully.");
}
}