5
votes

I am creating a WPF utility which needs to access the registry of the local machine, to then find out the installation path of the program.

I've navigated to the key via Regedit and it gives a Name, Type and Data, within the Data it shows the installation path, I would like to extract the installation path.

I know I need to navigate to this key within the registry:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\

then I need to access a folder within this key with the information regarding the installation path.

-

3
I only wish to read the key, to then extract the specific program installation path.user1569021
When you've come up with an answer, please post it as one instead of editing the original question. =)J. Steen
in most situations accessing the registry is discouraged, most of what you need can be found in the Application & Environment classes, msdn.microsoft.com/en-us/library/… msdn.microsoft.com/en-us/library/… or using a configfileMikeT

3 Answers

11
votes

I solved my problem, to anyone who wants a solution in the future if your still stuck after this please message me, I found it was hard to find the resources.

RegistryKey key = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\App Paths\myexe.exe");
string regFilePath = null;

object objRegisteredValue = key.GetValue("");

registeredFilePath = value.ToString();
0
votes

To read registry keys you should use Microsot.Windows.RegistryKey class, class Registry can open for you the RegistryKey.

0
votes

This question was very helpful to me. I came up with a helper class, wanting to play with the new Tuples.

Example usage:

public string SkypeExePath => InstalledApplicationPaths.GetInstalledApplicationPath( "lync.exe" );

The class:

public static class InstalledApplicationPaths
{

   public static string GetInstalledApplicationPath( string shortName )
   {
      var path = GetInstalledApplicationPaths().SingleOrDefault( x => x?.ExectuableName.ToLower() == shortName.ToLower() )?.Path;
      return path;
   }

   public static IEnumerable<(string ExectuableName, string Path)?> GetInstalledApplicationPaths()
   {
      using ( RegistryKey key = Registry.LocalMachine.OpenSubKey( @"Software\Microsoft\Windows\CurrentVersion\App Paths" ) )
      {
         foreach ( var subkeyName in key.GetSubKeyNames() )
         {
            using ( RegistryKey subkey = key.OpenSubKey( subkeyName ) )
            {
               yield return (subkeyName, subkey.GetValue( "" )?.ToString());
            }
         }
      }
   }

}