26. April 2013 08:45
Let's say you need to create a folder on behalf of a user and protect it from access by anyone but this user. Use DirectorySecurity.SetAccessRuleProtection procedure like this:
DirectoryInfo dir = new DirectoryInfo (DirPath);
DirectorySecurity ds = dir.GetAccessControl();
ds.SetAccessRuleProtection(true , false );
ds.AddAccessRule(new FileSystemAccessRule (DomainName + "\\\\" + UserName,
FileSystemRights .FullControl, AccessControlType .Allow));
Please be noted that local administrator could request and get access rights even for protected folders. Admins are admins.