Question

I have small problem with simple code. This code is working properly on "x86" mode but not on "Any CPU" mode, maybe it is possible to run one class on "x86" and another class on "Any CPU" mode? Code:

namespace Software_Info_v1._0
{
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;

public class Adobe
{
    public string GetAdobeVersion()
    {
        try
        {
            RegistryKey adobe = Registry.LocalMachine.OpenSubKey("Software").OpenSubKey("Adobe");
            if (adobe != null)
            {
                RegistryKey acroRead = adobe.OpenSubKey("Acrobat Reader");
                if (acroRead != null)
                {
                    string[] acroReadVersions = acroRead.GetSubKeyNames();
                    foreach (string versionNumber in acroReadVersions)
                    {
                        Console.WriteLine("Acrobat Reader version: " + versionNumber);
                    }
                }
            }
        }
        catch
        {
        }
        return null;
    }
}
}
Was it helpful?

Solution

This is because of registry redirection.

The structure of the registry is different for 32-bit and 64-bit OS.

Assuming you are running your application on a 64-bit machine, compiling for x86 target makes your program run using WOW64 mode (32-bit process on 64-bit) and you're reading keys under the Wow6432Node. See Weird behaviour when reading registry in C#

OTHER TIPS

When running as 32bit, the registry key gets redirected. When you run as 64bit, it won't get redirected, and thus won't hit the key to which adobe's registry entry got redirected anymore.

So I'd create a Find32BitRegEntry(string path) function, that does nothing on 32bit, and adds the redirect on x64.

The registry keys can be in a different place on 64 bit machines - see this. (Notice that RegistryKey in your sample code comes from Microsoft.Win32 ?)

I think you need to use a Registry Redirector, there's some talk about it over here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top