Search code examples
c#windowsms-wordregistryregistrykey

Why my program can't see that there is a registry key?


I am trying to check which versions of Word are installed on user's computer.

This is the code I am using:

    public static bool CheckIfWordVersionIsInstalled(int wordVersion)
    {
        var path32 = "Software\\Microsoft\\Office\\" + WordVersionKeyToValue(wordVersion) + "\\Word\\InstallRoot";
        var path64 = "Software\\Wow6432Node\\Microsoft\\Office\\" + WordVersionKeyToValue(wordVersion) + "\\Word\\InstallRoot";

        var key = Registry.LocalMachine.OpenSubKey(path32, RegistryKeyPermissionCheck.ReadSubTree);   
        Tools.Log("WordTools.CheckIfWordVersionIsInstalled: " + (key != null) + ": " + path32);

        if (key == null)
        {
            key = Registry.LocalMachine.OpenSubKey(path64, RegistryKeyPermissionCheck.ReadSubTree);
            Tools.Log("WordTools.CheckIfWordVersionIsInstalled: " + (key != null) + ": " + path64);
        }

        return key != null;
    }

In my log I can see keys that have been checked:

[16:36:57]WordTools.CheckIfWordVersionIsInstalled: False: Software\Microsoft\Office\16.0\Word\InstallRoot
[16:36:57]WordTools.CheckIfWordVersionIsInstalled: False: Software\Wow6432Node\Microsoft\Office\16.0\Word\InstallRoot
[16:36:57]WordTools.CheckIfWordVersionIsInstalled: False: Software\Microsoft\Office\15.0\Word\InstallRoot
[16:36:57]WordTools.CheckIfWordVersionIsInstalled: False: Software\Wow6432Node\Microsoft\Office\15.0\Word\InstallRoot
[16:36:57]WordTools.CheckIfWordVersionIsInstalled: False: Software\Microsoft\Office\14.0\Word\InstallRoot
[16:36:57]WordTools.CheckIfWordVersionIsInstalled: False: Software\Wow6432Node\Microsoft\Office\14.0\Word\InstallRoot
[16:36:57]WordTools.CheckIfWordVersionIsInstalled: False: Software\Microsoft\Office\12.0\Word\InstallRoot
[16:36:57]WordTools.CheckIfWordVersionIsInstalled: False: Software\Wow6432Node\Microsoft\Office\12.0\Word\InstallRoot
[16:36:57]WordTools.CheckIfWordVersionIsInstalled: False: Software\Microsoft\Office\11.0\Word\InstallRoot
[16:36:57]WordTools.CheckIfWordVersionIsInstalled: False: Software\Wow6432Node\Microsoft\Office\11.0\Word\InstallRoot

You can see that all of them returned False. But when I open my registry, I can clearly see that there is an existing key: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\16.0\Word\InstallRoot

How is that possible? My program is compiled as x86 (I tried also Any CPU but with the same result). Word is also x86.


Solution

  • Actually AnyCPU should work fine (on 64-bit Windows). For sure x86 does not work. When you run a 32-bit program then all access to Registry key HKEY_LOCAL_MACHINE\Software are automatically redirected to HKEY_LOCAL_MACHINE\Software\WOW6432Node, thus you have no access to HKEY_LOCAL_MACHINE\Software from a 32-bit application.

    See also Registry Redirector

    Apart from that you mixed the path. Wow6432Node is used for 32bit not vice versa.

    Update:

    My Statement "you have no access to HKEY_LOCAL_MACHINE\Software from a 32-bit application" is not fully correct. You can access them, however looks like you have to use the API call, you cannot access them directly by Registry Class, see Accessing an Alternate Registry View