SetValue 64bit machine registry

2019-02-19 00:29发布

I want to set a value for 'NoModify' in below registry path. "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\XXXX"

I am using below code and it works only for X86 machines. Can you see any reason why this is not working for x64 machines?

// This value is correct
RegistryView registryView = releaseFlags.Contains("PLATFORM_X86") ? RegistryView.Registry64 : RegistryView.Registry32;

    using (RegistryKey hkeyLocalMachine = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, registryView))
    {
        RegistryKey noModifyKey = hkeyLocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{xxxx}", true); //SL: Key Name

        if (noModifyKey != null)
        {
            noModifyKey.SetValue("NoModify", 0);
            noModifyKey.Flush();
        }
    }

3条回答
来,给爷笑一个
2楼-- · 2019-02-19 01:15

When you are on a 64 bit machine and your app is 32 bit - it should store these settings in the HKLM\Software\WOW6432Node instead of the HKLM\Software\ node.

查看更多
Juvenile、少年°
3楼-- · 2019-02-19 01:18

As far as you compiles your .NET program as x86 not AnyCPU, you will be using the "correct" registry keys meant for x86 in any circumstances because it will be running as x86.

If you compile it as x64 or AnyCPU, it could be quite tricky because it will probably run as x64 on an x64 machine and uses the "wrong" registry where HKLM\SOFTWARE for x86 programs is actually HKLM\SOFTWARE\WOW6432Node.

查看更多
叼着烟拽天下
4楼-- · 2019-02-19 01:24

It's my mistake in the code.

RegistryView registryView = releaseFlags.Contains("PLATFORM_X86") ? RegistryView.Registry64 : RegistryView.Registry32;

Should be as follows:

RegistryView registryView = releaseFlags.Contains("PLATFORM_X86") ? RegistryView.Registry32 : RegistryView.Registry64;
查看更多
登录 后发表回答