繁体   English   中英

如何以编程方式编辑特定用户的注册表项?

[英]How to edit the registry keys of a specific user programatically?

我想更改我在应用程序中创建的Windows用户的一些设置。 如果我理解正确,他的“ HKEY_CURRENT_USER ”值将在HKEY_USERS/<sid>/... 这个对吗? 如果我知道用户名和域名,我怎样才能获得用户的sid?

编辑:如果我已经有sid,我该如何正确编辑该用户的HKCU密钥?

我有一个程序正是这样做的。 以下是代码的相关部分:

NTAccount ntuser = new NTAccount(strUser);
SecurityIdentifier sID = (SecurityIdentifier) ntuser.Translate(typeof(SecurityIdentifier));
strSID = sID.ToString();

您需要导入两个名称空间:

using System.DirectoryServices;
using System.Security.Principal;

希望这可以帮助。

然后使用带有SID字符串\\ path的Registry.Users.SetValue来设置注册表值。

如果您正在编辑已注销的配置文件,尤其是漫游配置文件,则可能无法按预期工作。

这有两个步骤。 首先,您必须获得用户sid。 其次,您必须加载用户注册表配置单元。 默认情况下不会加载其他用户配置单元,因此您必须显式加载它。

Daniel White的评论中的答案是获得sid的最佳方式。

要加载用户的注册表配置单元,请通过pinvoke使用LoadUserProfile Windows API。 完成后,有一个互补的UnloadUserProfile可以卸载配置单元。

您可以使用Query by example并使用PrincipalSearcher搜索适当的UserPrincipal

// Since you know the domain and user
PrincipalContext context = new PrincipalContext(ContextType.Domain);

// Create the principal user object from the context
UserPrincipal usr = new UserPrincipal(context);
usr .GivenName = "Jim";
usr .Surname = "Daly";

// Create a PrincipalSearcher object.
PrincipalSearcher ps = new PrincipalSearcher(usr);
PrincipalSearchResult<Principal> results = ps.FindAll();
foreach (UserPrincipal user in results) {
    if(user.DisplayName == userName) {
        var usersSid = user.Sid.ToString();
    }
}

暂无
暂无

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

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