简体   繁体   中英

Different behavior reading the registry when opening the solution in VS2015 and VS2017

I have a solution that I wanted to upgrade from VS 2015 to VS 2017.

The program in the solution will read values from registry.

For example, the path is : Computer\\HKEY_LOCAL_MACHINE\\SOFTWARE\\MYPROGRAM

When opening the solution using VS 2015, it can read the registry successfully. But when opening the solution using VS 2017, it seems to read the registry from Computer\\HKEY_LOCAL_MACHINE\\SOFTWARE\\WOW6432Node\\MYPROGRAM

The code

private const string REG_KEY = "Software\\MYPROGRAM";

public static string GetEnvironment()
{
    if (SystemEnvironment == string.Empty)
    {
        RegistryKey rk = Registry.LocalMachine.OpenSubKey(REG_KEY);
        if (rk == null)
            throw new Exception("Unable to open registry key");
        else
            try
            {
                SystemEnvironment = rk.GetValue("ENVIRONMENT").ToString();
                if (SystemEnvironment == null)
                    throw new Exception("Unable to retrieve Environment");
            }
            finally
            { rk.Close(); }
    }
    return SystemEnvironment;
}

It's the same code, the difference is the VS version. How to fix the issue?

It turns out that my settings for VS2015 and VS2017 is different.

To solve this, I need to set my Visual Studio to use the 64 bit version of IIS Express. To do that, Go to Tools > Options > Projects and Solutions > Web Projects > Tick "Use the 64 bit version of IIS Express for web sites and projects".

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM