Problem setting file system access rule

Farna picture Farna · May 9, 2011 · Viewed 8k times · Source

In my app on local network, any user should create a directory on shared folder using this code. test1 is the name of one of the user's folder for example.

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

Now when the admin in domain wants to read every directory from any user this error ocurred:

Attempted to perform an unauthorized operation

The code that the admin runs is:

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

Where is my mistake?
Thanks in advance.

Answer

PraveenVenu picture PraveenVenu · May 10, 2011

Can you try like the below

        // 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);