0

In my c# program, I'm running a python process. Currently I'm using a hardcoded path for the python.exe, but I want to use the windows registry to return the path to me.

I have found the python path info in the Windows registry under: HKEY_CURRENT_USER\Software\Python\PythonCore\3.7-32\InstallPath

with some googling I found the following solution: https://stackoverflow.com/a/18234755/7183609

but when I run my code the variable key is always null

try
{
    using (RegistryKey key = Registry.LocalMachine.OpenSubKey("Software\\Python\\PythonCore\\3.7-32\\InstallPath"))
    {
        if (key != null)
        {
            // do things
        }
    }
}
catch (Exception ex)
{
    // do other things
}

Am I doing something wrong, do I need to add something to make it work?

Roger Lipscombe
  • 89,048
  • 55
  • 235
  • 380
wes
  • 379
  • 3
  • 15

2 Answers2

1

John Wu pointed out that LocalMachine was wrong.

changed

using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"HKEY_CURRENT_USER\Software\Python\PythonCore\3.7-32\InstallPath"))

to

using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"HKEY_CURRENT_USER\Software\Python\PythonCore\3.7-32\InstallPath"))
wes
  • 379
  • 3
  • 15
0

Use Registry.CurrentUser instade of Registry.LocalMachine

try
    {
        using (RegistryKey key = Registry.CurrentUser.OpenSubKey("Software\\Python\\PythonCore\\3.7-32\\InstallPath"))
        {
            if (key != null)
            {
                // do things
            }
        }
    }
    catch (Exception ex)
    {
        // do other things
    }
Sait ORHAN
  • 1
  • 1
  • 1