Search code examples
c#registryinformation-retrievalregistrykey

How to retrieve an installed file path from Registry


I would like to retrieve the path of an installed software from registry, I have tried different methods i.e. this one and this too but I faced the same error:

Object reference not set to an instance of an Object.

I observed that Registry.LocalMachine.OpenSubKey(registry_key) returns null so I searched for the solution of this problem and found many, but those could not solve my issue. I want to continue with the following code:

string txt_filePath = "";

RegistryKey key = Registry.LocalMachine.OpenSubKey(@"HKEY_LOCAL_MACHINE\SOFTWARE\Bentley\AutoPIPE\V8i SELECTSeries 6 Ribbon Preview and Reporting");
object objRegisteredValue = key.GetValue("ApplicationPath");

txt_filePath = objRegisteredValue.ToString();

Any help will be highly appreciated.


Solution

  • Maybe trouble is because of x64-bit machine? Try this:

        var regView = Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32;
        var registry = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, regView);
    
        var keyPath = @"HKEY_LOCAL_MACHINE\SOFTWARE\Bentley\AutoPIPE\V8i SELECTSeries 6 Ribbon Preview and Reporting\RibbonUI.xml";
        var key = registry.OpenSubKey(keyPath);