2

在我在本地网络上的应用程序中,任何用户都应该使用此代码在共享文件夹上创建一个目录。例如,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);

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

4

1 回答 1

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);
于 2011-05-10T07:35:15.363 回答