Reading a registry key in C #

I developed the application and installed it on the client computer. In my application, I need to install its installation path. My application has a registry entry at:

HKEY_LOCAL_MACHINE\SOFTWARE\MyApplication\[AppPath] 

How to read AppPath using C #?

+45
c # registry
Dec 17 '10 at 3:41
source share
6 answers

see this http://www.codeproject.com/Articles/3389/Read-write-and-delete-from-registry-with-C

Updated:

You can use the RegistryKey class in the Microsoft.Win32 namespace.

Some important functions of RegistryKey are as follows:

 GetValue //to get value of a key SetValue //to set value to a key DeleteValue //to delete value of a key OpenSubKey //to read value of a subkey (read-only) CreateSubKey //to create new or edit value to a subkey DeleteSubKey //to delete a subkey GetValueKind //to retrieve the datatype of registry key 
+24
Dec 17 '10 at 3:50
source share

You are looking for a cleverly named Registry.GetValue method .

+107
Dec 17 2018-10-12T00:
source share
 string InstallPath = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\MyApplication\AppPath", "Installed", null); if (InstallPath != null) { // Do stuff } 

This code should get your value. You need to be

 using Microsoft.Win32; 

for this to work though.

+70
Dec 17 2018-10-12T00:
source share

You can use the following to find out where the registry thinks it is installed:

 (string)Registry.LocalMachine.GetValue(@"SOFTWARE\MyApplication\AppPath", "Installed", null); 

Or you can use the following to find out where the application actually starts:

 System.Windows.Forms.Application.StartupPath 

The latter is more reliable than the first if you are trying to use the .exe location as a relative path to search for related files. The user can easily move things after installation and still work in the application, because .NET applications are not so dependent on the registry.

Using StartupPath , you can even do something smart, as if your application was updating registry entries at runtime rather than failing due to missing / incorrect / corrupted entries.

And do not forget to look at the settings of the application, as a storage of values, and not on the registry ( Properties.Settings.Default.mySettingEtc ). You can read / write settings for applications and / or user levels, which are saved as simple MyApp.exe.config files in standard places. A good take-off from the past (the good old days of Win 3.1 / DOS) for installing / uninstalling an application is a simple copy / uninstall of a folder structure or two, and not some confusing, secret installation / uninstallation procedure that leaves all kinds of garbage in the registry and sprinkled on the entire hard drive.

+7
04 Oct
source share

If you want it to be selected by a specific type, you can use this method. Most non-primitive types do not support direct casting by default, so you have to handle them accordingly.

  public T GetValue<T>(string registryKeyPath, string value, T defaultValue = default(T)) { T retVal = default(T); retVal = (T)Registry.GetValue(registryKeyPath, value, defaultValue); return retVal; } 
+5
Aug 19 '13 at 14:46
source share

using Microsoft.Win32;

  string chkRegVC = "NO"; private void checkReg_vcredist() { string regKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"; using (Microsoft.Win32.RegistryKey uninstallKey = Registry.LocalMachine.OpenSubKey(regKey)) { if (uninstallKey != null) { string[] productKeys = uninstallKey.GetSubKeyNames(); foreach (var keyName in productKeys) { if (keyName == "{196BB40D-1578-3D01-B289-BEFC77A11A1E}" ||//Visual C++ 2010 Redistributable Package (x86) keyName == "{DA5E371C-6333-3D8A-93A4-6FD5B20BCC6E}" ||//Visual C++ 2010 Redistributable Package (x64) keyName == "{C1A35166-4301-38E9-BA67-02823AD72A1B}" ||//Visual C++ 2010 Redistributable Package (ia64) keyName == "{F0C3E5D1-1ADE-321E-8167-68EF0DE699A5}" ||//Visual C++ 2010 SP1 Redistributable Package (x86) keyName == "{1D8E6291-B0D5-35EC-8441-6616F567A0F7}" ||//Visual C++ 2010 SP1 Redistributable Package (x64) keyName == "{88C73C1C-2DE5-3B01-AFB8-B46EF4AB41CD}" //Visual C++ 2010 SP1 Redistributable Package (ia64) ) { chkRegVC = "OK"; break; } else { chkRegVC = "NO"; } } } } } 
+1
Jun 03 '16 at 5:41
source share



All Articles