问题设置文件系统访问规则

时间:2011-05-09 12:42:56

标签: c# winforms directoryinfo local-network access-rights

在我的本地网络应用中,任何用户都应使用此代码在共享文件夹上创建目录。例如,test1是用户文件夹之一的名称。

DirectoryInfo di = new DirectoryInfo(@"\\Server\Test\test1");
DirectorySecurity ds=new DirectorySecurity();
ds.SetAccessRule(new FileSystemAccessRule(Enviroment.Username,
                       FileSystemRights.FullControl, AccessControlType.Deny)); 
di.Create(ds);

现在,当域管理员想要从任何用户读取每个目录时,会出现此错误:

  

尝试执行未经授权的操作

管理员运行的代码是:

DirectoryInfo di = new DirectoryInfo(@"\\Server\Test\test1");
DirectorySecurity ds=new DirectorySecurity();
ds.SetAccessRule(new FileSystemAccessRule(Enviroment.Username,
                       FileSystemRights.FullControl, AccessControlType.Allow)); 
di.SetAccessControl(ds);

我的错误在哪里?
提前谢谢。

1 个答案:

答案 0 :(得分:4)

你可以试试下面的

        // Create a new DirectoryInfo object.
        DirectoryInfo dInfo = new DirectoryInfo(@"\\Server\Test\test1");

        // 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(Enviroment.Username,
                                                        FileSystemRights.FullControl, AccessControlType.Allow));

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