0

我在存储文件夹权限时遇到了一点麻烦。我能够找到一些关于编写和阅读它们的文档。我要做的是读取特定用户的文件夹权限>存储它>更改权限>安装程序完成后,将权限更改回来。

除了如何存储原始文件夹权限并将其重新设置外,我已将所有内容都记录下来(仅由于许多其他人的代码)。我很乐意阅读您建议的任何材料,我们收到了该软件的几个致命错误,这是解决其中许多错误的一步。欢迎并感谢所有帮助。

下面是我如何设置权限的示例。是的,我知道我有每个人,但现在只是为了测试

 public void setPermDir()
    {
        try
        {
            string DirectoryName = "C:\\Temp1\\";

            Console.WriteLine("Adding access control entry for " + DirectoryName);

            // Add the access control entry to the directory.
            AddDirectorySecurity(DirectoryName, @"Everyone", FileSystemRights.FullControl, AccessControlType.Allow);


            Console.WriteLine("Done.");
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }

        Console.ReadLine();
    }

    // Adds an ACL entry on the specified directory for the specified account.
    public static void AddDirectorySecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType)
    {
        // Create a new DirectoryInfo object.
        DirectoryInfo dInfo = new DirectoryInfo(FileName);

        // Get a DirectorySecurity object that represents the 
        // current security settings.
        DirectorySecurity dSecurity = dInfo.GetAccessControl();

        // Add the FileSystemAccessRule to the security settings. 
        dSecurity.AddAccessRule(new FileSystemAccessRule(Account,
                                                        Rights,
                                                        ControlType));

        // Set the new access settings.
        dInfo.SetAccessControl(dSecurity);

    }
4

1 回答 1

0

如果您从 AddDirectorySecurity 返回 DirectorySecurity dSecurity,那么您可以Directory.SetAccessControl(directoryName, dSecurity);在完成修改后的访问规则后调用。

更新

如果只是 SetAccessControl 不起作用,下一步可能是显式删除您使用FileSystemSecurity.RemoveAccessRule授予的权限。

只需保留对您创建的 FileSystemAccessRule 的引用:

FileSystemAccessRule toRemoveWhenDone = new FileSystemAccessRule(Account, Rights, ControlType);
于 2010-11-28T19:27:59.850 回答