[英]Registry get directory owner programmatically using C#
I want to read programmatically the owner of a directory (and its subdirectories) of the windows registry using C#. 我想以编程方式使用C#阅读Windows注册表的目录(及其子目录)的所有者。
For example, assume my registry contains the directory HKEY_CURRENT_USER\\Software\\Microsoft
which is owned by the user SYSTEM
. 例如,假设我的注册表包含目录
HKEY_CURRENT_USER\\Software\\Microsoft
这是由用户拥有SYSTEM
。 A code example (leaving out the recursion over sub-directories of dir
) how I intend to use it would be: 我打算如何使用它的代码示例(省略
dir
子目录的递归)将是:
string dir = @"HKEY_CURRENT_USER\Software\Microsoft";
string owner = ReadRegOwner(dir); // owner is "SYSTEM"
However, I am not sure how to implement ReadRegOwner
in C#. 但是,我不确定如何在C#中实现
ReadRegOwner
。 I have already found the RegistrySecurity class, but I am not sure how to use it to get the owner of a registry directory. 我已经找到了RegistrySecurity类,但我不知道如何使用它来获取注册表目录的所有者。 It has the
GetOwner
member function, but that function requires an argument of type Type
and I am not sure what to pass there. 它具有
GetOwner
成员函数,但是该函数需要类型为Type
的参数,我不确定该向那里传递什么。
Does anyone know how to implement this? 有人知道如何实现吗?
So, an implementation could look like: 因此,实现可能如下所示:
string ReadRegOwner(string dir)
{
RegistryKey key = Registry.CurrentUser.OpenSubKey(dir, false);
RegistrySecurity rs = key.GetAccessControl();
IdentityReference owner = rs.GetOwner(typeof(System.Security.Principal.NTAccount));
return owner.ToString();
}
Example: 例:
string dir = @"Software\Microsoft";
string owner = ReadRegOwner(dir); // Looks in HKEY_CURRENT_USER
Of course, CurrentUser
could be also replaced if a different base key than HKEY_CURRENT_USER
is desired. 当然,如果需要与
HKEY_CURRENT_USER
不同的基本密钥,也可以替换CurrentUser
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.