77

我使用下面的代码允许所有人访问文件夹:

System.Security.AccessControl.DirectorySecurity sec =
    System.IO.Directory.GetAccessControl(directory, AccessControlSections.All);
FileSystemAccessRule accRule = new FileSystemAccessRule("Everyone",
                                       FileSystemRights.Modify,
                                       AccessControlType.Allow);
sec.AddAccessRule(accRule);    // setACL
sec.ResetAccessRule(accRule);

现在,Everyone 用户已添加到文件夹中,但未分配任何权限。所有读取、写入、执行等复选框均未选中。

4

3 回答 3

141

我想告诉你的第一件事是我是如何找到这个解决方案的。这可能比答案更重要,因为很难获得正确的文件权限。

我做的第一件事是使用 Windows 对话框和复选框设置我想要的权限。我为“所有人”添加了一条规则,并勾选了除“完全控制”之外的所有框。

然后我编写了这段 C# 代码来准确地告诉我复制 Windows 设置需要哪些参数:

string path = @"C:\Users\you\Desktop\perms"; // path to directory whose settings you have already correctly configured
DirectorySecurity sec = Directory.GetAccessControl(path);
foreach (FileSystemAccessRule acr in sec.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount))) {
    Console.WriteLine("{0} | {1} | {2} | {3} | {4}", acr.IdentityReference.Value, acr.FileSystemRights, acr.InheritanceFlags, acr.PropagationFlags, acr.AccessControlType);
}

这给了我这行输出:

Everyone | Modify, Synchronize | ContainerInherit, ObjectInherit | None | Allow

所以解决方案很简单(但如果您不知道要寻找什么,就很难做到正确!):

DirectorySecurity sec = Directory.GetAccessControl(path);
// Using this instead of the "Everyone" string means we work on non-English systems.
SecurityIdentifier everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
sec.AddAccessRule(new FileSystemAccessRule(everyone, FileSystemRights.Modify | FileSystemRights.Synchronize, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
Directory.SetAccessControl(path, sec);

这将使 Windows 安全对话框上的复选框与您已经为测试目录设置的复选框相匹配。

于 2011-03-22T22:01:10.210 回答
15

下面的代码检查文件夹是否存在,如果没有创建,则创建一个。然后将该文件夹的每个用户权限设置为具有完全权限(读取和写入)。

string file = @"D:\Richi";     
private static void GrantAccess(string file)
            {
                bool exists = System.IO.Directory.Exists(file);
                if (!exists)
                {
                    DirectoryInfo di = System.IO.Directory.CreateDirectory(file);
                    Console.WriteLine("The Folder is created Sucessfully");
                }
                else
                {
                    Console.WriteLine("The Folder already exists");
                }
                DirectoryInfo dInfo = new DirectoryInfo(file);
                DirectorySecurity dSecurity = dInfo.GetAccessControl();
                dSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.NoPropagateInherit, AccessControlType.Allow));
                dInfo.SetAccessControl(dSecurity);

            }
于 2016-08-02T12:23:33.920 回答
5

如果要允许所有操作 (ACL),请使用FileSystemRights.FullControl而不是。FileSystemRights.Modify

于 2011-03-14T13:46:07.410 回答