如果不存在,将密钥添加到注册表
Add key to registry if not exist
如果注册表项不存在,我尝试将其添加到注册表中。
虽然我调试一切都很好。代码应该工作。但是我在注册表编辑器中找不到密钥。你有什么主意吗?
public void ConfigureWindowsRegistry()
{
var reg = Registry.LocalMachine.OpenSubKey("Software\Microsoft\Office\Outlook\FormRegions\tesssst", true);
if (reg == null)
{
reg = Registry.LocalMachine.CreateSubKey("Software\Microsoft\Office\Outlook\FormRegions\tesssst");
}
if (reg.GetValue("someKey") == null)
{
reg.SetValue("someKey", "someValue");
}
}
如果您使用的是 64 位 OS,一些注册表项会被 WOW64 重定向。 MSDN 上提供了有关此主题的更多信息,您应该查看 Wow6432Node 下,您会找到您的条目。如果您第一次执行该代码,它将在 64 位机器上创建(我在本地尝试过),此条目:
HKEY_LOCAL_MACHINE\Software\Wow6432Node \Microsoft\Office\Outlook\FormRegions\tesssst
如果您想访问注册表的 64 位部分,您应该这样做:
public void ConfigureWindowsRegistry()
{
RegistryKey localMachine = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64); //here you specify where exactly you want your entry
var reg = localMachine.OpenSubKey("Software\Microsoft\Office\Outlook\FormRegions\tesssst",true);
if (reg == null)
{
reg = localMachine.CreateSubKey("Software\Microsoft\Office\Outlook\FormRegions\tesssst");
}
if (reg.GetValue("someKey") == null)
{
reg.SetValue("someKey", "someValue");
}
}
执行上面的代码会将注册表项放入您定位的正确部分。
希望对您有所帮助。
如果注册表项不存在,我尝试将其添加到注册表中。 虽然我调试一切都很好。代码应该工作。但是我在注册表编辑器中找不到密钥。你有什么主意吗?
public void ConfigureWindowsRegistry()
{
var reg = Registry.LocalMachine.OpenSubKey("Software\Microsoft\Office\Outlook\FormRegions\tesssst", true);
if (reg == null)
{
reg = Registry.LocalMachine.CreateSubKey("Software\Microsoft\Office\Outlook\FormRegions\tesssst");
}
if (reg.GetValue("someKey") == null)
{
reg.SetValue("someKey", "someValue");
}
}
如果您使用的是 64 位 OS,一些注册表项会被 WOW64 重定向。 MSDN 上提供了有关此主题的更多信息,您应该查看 Wow6432Node 下,您会找到您的条目。如果您第一次执行该代码,它将在 64 位机器上创建(我在本地尝试过),此条目:
HKEY_LOCAL_MACHINE\Software\Wow6432Node \Microsoft\Office\Outlook\FormRegions\tesssst
如果您想访问注册表的 64 位部分,您应该这样做:
public void ConfigureWindowsRegistry()
{
RegistryKey localMachine = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64); //here you specify where exactly you want your entry
var reg = localMachine.OpenSubKey("Software\Microsoft\Office\Outlook\FormRegions\tesssst",true);
if (reg == null)
{
reg = localMachine.CreateSubKey("Software\Microsoft\Office\Outlook\FormRegions\tesssst");
}
if (reg.GetValue("someKey") == null)
{
reg.SetValue("someKey", "someValue");
}
}
执行上面的代码会将注册表项放入您定位的正确部分。
希望对您有所帮助。