0

When setting the owner of a preexisting folder in Server 2008 R2, is there a way to make this propagate to all of the sub containers under the object? You can do this in the Properties diag, but I do not see a switch for this.

I am looking for a way to avoid recusing through the sub containers to do this.

This code will switch the owner for only the top level directory.

DirectoryInfo myDirectoryInfo = new DirectoryInfo("PATH HERE");
DirectorySecurity myDirectorySecurity = myDirectoryInfo.GetAccessControl();

System.Security.Principal.IdentityReference myOwner = 
    new System.Security.Principal.NTAccount("TARGET OWNER ACCOUNT");
myDirectorySecurity.SetOwner(myOwner);

myDirectoryInfo.SetAccessControl(myDirectorySecurity);
Jesse
  • 434
  • 2
  • 6
  • 16

1 Answers1

0

There is no overload that will provide this for you.

But the following code will do that:

DirectoryInfo directoryInfo = new DirectoryInfo(path);
var directories = directoryInfo.EnumerateDirectories("*.*", SearchOption.AllDirectories);
foreach (var directory in directories)
{
    // set owner                
}

Note that I use EnumerateDirectories instead of Directory.GetDirectories, it will return instantly even if there are 1000's of directories.

aybe
  • 15,516
  • 9
  • 57
  • 105
  • It will return them all, but then you must set the owner on each one in the foreach statement. Meaning more run time to do that. – Jesse Sep 10 '14 at 17:32
  • This operation takes time as well in Windows, if you are concerned with UI freezes or whatever, run the method asynchronously and report progress using Progress. – aybe Sep 10 '14 at 17:58