Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading a registry key in C#

Tags:

c#

registry

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

HKEY_LOCAL_MACHINE\SOFTWARE\MyApplication\[AppPath] 

How can I read AppPath using C#?

like image 788
Thorin Oakenshield Avatar asked Dec 17 '10 03:12

Thorin Oakenshield


People also ask

How do I read registry keys?

You can use Get-ChildItem to view registry keys and Set-Location to navigate to a key path. Registry values are attributes of a registry key. In the Registry drive, they are called Item Properties. A registry key can have both children keys and item properties.

How do I read a value from the Windows Registry?

Use the GetValue method, specifying the path and name) to read a value from registry key. The following example reads the value Name from HKEY_CURRENT_USER\Software\MyApp and displays it in a message box.

Is a 1 or a 0 true in the registry?

Usually 1 is true and 0 is false. However, this depends on how the application is implemented.


2 Answers

You're looking for the cunningly named Registry.GetValue method.

like image 141
SLaks Avatar answered Sep 20 '22 18:09

SLaks


string InstallPath = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\MyApplication\AppPath", "Installed", null);     if (InstallPath != null) {     // Do stuff } 

That code should get your value. You'll need to be

using Microsoft.Win32; 

for that to work though.

like image 43
Migwell Avatar answered Sep 19 '22 18:09

Migwell