[英]Set Owner of Directory and Sub Containers in C#
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? 在Server 2008 R2中设置一个预先存在的文件夹的所有者时,是否有办法使它传播到对象下的所有子容器? 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);
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. 请注意,我使用
EnumerateDirectories
代替Directory.GetDirectories
,即使有1000个Directory.GetDirectories
,它也会立即返回。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.