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?
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.
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.
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 .
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.
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:
RegistryKey
reference will keep the registry open. Using that as a static variable in the class will cause issues on the computer.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 } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With