简体   繁体   English

子容器和对象上的 C# DirectorySecurity SetOwner

[英]C# DirectorySecurity SetOwner on subcontainers and objects

I am trying to change ownership of a windows folder and everything inside it.我正在尝试更改 Windows 文件夹及其中所有内容的所有权。 Basially I am trying to check the box that would be there if you did this manually in windows that says "Replace owner on subcontainers and objects".基本上,如果您在显示“替换子容器和对象的所有者”的窗口中手动执行此操作,我会尝试选中该框。 This will need to work over a network share path.这需要在网络共享路径上工作。 I am able to get 1 folder deep but then it just stops there.我能够深入 1 个文件夹,但它就停在那里。 This does not include the base folder changing either.这也不包括更改基本文件夹。

 foreach (string directory in Directory.GetDirectories(dirPath))
            {
                var di = new DirectoryInfo(directory);
                IdentityReference user = new NTAccount(Login.authUserName.ToString());
                DirectorySecurity dSecurity = di.GetAccessControl();
                dSecurity.SetOwner(user);
                di.SetAccessControl(dSecurity);
            }

You can use Directory.GetDirectories with SearchOption.AllDirectories in order to recurse.您可以将Directory.GetDirectoriesSearchOption.AllDirectories一起使用以进行递归。

But it seems easier to just get the DirectoryInfo di objects directly using DirectoryInfo.GetDirectories , which has the same and more recursive options.但是使用DirectoryInfo.GetDirectories直接获取DirectoryInfo di对象似乎更容易,它具有相同且更递归的选项。

IdentityReference user = new NTAccount(Login.authUserName.ToString());

var root = new DirectoryInfo(dirPath);
foreach (var di in root.GetDirectories("*", SearchOption.TopDirectoryOnly).Prepend(root))
{
    var dSecurity = di.GetAccessControl();
    dSecurity.SetOwner(user);
    di.SetAccessControl(dSecurity);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM