Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read a Registry Key

Tags:

I have a web application which is importing DLLs from the bin folder.

const string dllpath = "Utility.dll";      [DllImport(dllpath)] 

Now what I want to do is first import the DLLs from a folder not in the current project but at some different location.

The path of that folder is stored in a registry key.

How should I do this?

Edit:

Why can't I work this out???

public partial class Reports1 : System.Web.UI.Page {      RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(@"Software\xyz");     string pathName = (string)registryKey.GetValue("BinDir");      const string dllpath = pathName;     [DllImport(dllpath)]     public static extern bool GetErrorString(uint lookupCode, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder buf, uint bufSize);      protected void Page_Load(object sender, EventArgs e)     { 

string pathName = (string)registryKey.GetValue("BinDir"); is not working here, but is working in the pageload event...

But if I do this DLL import won't work... How can I fix this?

like image 566
user175084 Avatar asked Nov 04 '09 18:11

user175084


People also ask

How do I read registry keys?

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.

How do I read a registry file?

To view the contents of a REG file, right-click it in File Explorer and select “Edit.” This will open it in Notepad. Note: If you don't see the “Edit” option, the REG file may be inside a ZIP archive. You may need to extract the REG file from the ZIP archive before continuing.

How do I read a registry value in PowerShell?

One of the easiest ways to find registry keys and values is using the Get-ChildItem cmdlet. This uses PowerShell to get a registry value and more by enumerating items in PowerShell drives. In this case, that PowerShell drive is the HKLM drive found by running Get-PSDrive .

What is a registry key?

In the Windows 98, CE, NT, and 2000 operating systems, a registry key is an organizational unit in the Windows registry, an internal database the computer uses to store configuration information.


2 Answers

Reading the registry is pretty straightforward. The Microsoft.Win32 namespace has a Registry static class. To read a key from the HKLM node, the code is:

RegistryKey registryKey = Registry.LocalMachine.OpenSubKey("Software\\NodeName") 

If the node is HKCU, you can replace LocalMachine with CurrentUser.

Once you have the RegistryKey object, use GetValue to get the value from the registry. Continuing Using the example above, getting the pathName registry value would be:

string pathName = (string) registryKey.GetValue("pathName"); 

And don't forget to close the RegistryKey object when you are done with it (or put the statement to get the value into a Using block).

Updates

I see a couple of things. First, I would change pathName to be a static property defined as:

Private static string PathName {      get     {          using (RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(@"Software\Copium"))          {               return (string)registryKey.GetValue("BinDir");          }     } } 

The two issues were:

  1. The RegistryKey reference will keep the registry open. Using that as a static variable in the class will cause issues on the computer.
  2. Registry path's use forward slashes, not back slashes.
like image 102
Jeff Siver Avatar answered Sep 23 '22 20:09

Jeff Siver


None of these answers worked for me. This is what I used:

static void Main() {     const string dotNetFourPath = "Software\\Microsoft";//note backslash     using (RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(dotNetFourPath))     {         Console.WriteLine(registryKey.SubKeyCount);//registry is not null         foreach (var VARIABLE in registryKey.GetSubKeyNames())         {             Console.WriteLine(VARIABLE);//here I can see I have many keys             //no need to switch to x64 as suggested on other posts         }     } } 
like image 36
P.Brian.Mackey Avatar answered Sep 25 '22 20:09

P.Brian.Mackey