默认情况下,DefaultUserName和DefaultPassword不会保留Windows 7上的注册表编辑

时间:2016-05-07 09:06:41

标签: c# windows-7 registry

我编写了一个C#应用程序来更改Windows 7上的注册表值AutoLogon,DefaultUserName和DefaultPassword。

完整路径为" HKEY_LOCAL_MACHINE \ Software \ Microsoft \ Windows NT \ CurrentVersion \ WinLogon"

RegistryKey key = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows NT\\CurrentVersion\\WinLogon", true);
if(key != null)
{      
     key.SetValue("DefaultUserName", "username");
     key.SetValue("DefaultPassword", "password");
}

此代码执行时没有任何错误或异常,但注册表中没有受到影响的更改。

我已经作为管理员执行,执行此操作的系统有一个用户,而且是管理员。

1 个答案:

答案 0 :(得分:0)

请务必使用key.Close(),否则将永远不会保存。

像这样:

RegistryKey key = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows NT\\CurrentVersion\\WinLogon", true);

if(key != null)
{
    key.SetValue("DefaultUserName", "username");
    key.SetValue("DefaultPassword", "password");
    key.Close();
}

这是MS的链接:https://msdn.microsoft.com/en-GB/library/h5e7chcf.aspx