阅读注册表项

我有一个Web应用程序,它从bin文件夹导入DLL。

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

现在我想要做的是首先从不在当前项目中但在某个不同位置的文件夹中导入DLL。

该文件夹的路径存储在注册表项中。

我该怎么做?

编辑

为什么我不能解决这个问题?

 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"); 不在这里工作,但正在页面加载事件…

但是,如果我这样做DLL导入将无法正常工作……我该如何解决这个问题?

阅读注册表非常简单。 Microsoft.Win32命名空间具有Registry静态类。 要从HKLM节点读取密钥,代码为:

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

如果节点是HKCU ,则可以将LocalMachine替换为CurrentUser

获得RegistryKey对象后,使用GetValue从注册表中获取值。 继续使用上面的示例,获取pathName注册表值将是:

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

并且不要忘记在完成后关闭RegistryKey对象(或者将语句放入Using块中)。

更新

我看到了几件事。 首先,我将pathName更改为静态属性,定义为:

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

这两个问题是:

  1. RegistryKey引用将使注册表保持打开状态。 将其用作类中的静态变量将导致计算机出现问题。
  2. 注册表路径使用正斜杠,而不是反斜杠。

这些答案都不适合我。 这是我用过的:

 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 } } } 

所有这些答案都可能导致在64位操作系统上运行时出现问题 – 这在当今很常见。

在我的情况下,我编译为’任何CPU’目标,当我在64位操作系统上安装时,软件工作正常。 但我的unit testing遇到了问题 – 显然它们是在32位模式下执行的。

在这种情况下,不会搜索HKEY_LOCAL_MACHINE\SOFTWARE\MyCompany\MySoftware ,但HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\MyCompany\MySoftware但没有条目!

在这种情况下,我们必须使用指定搜索的起点

 RegistryKey hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64) 

我们总共可以使用。

 string configurationDirectory = string.Empty; using (RegistryKey hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)) { using (RegistryKey registryKey = hklm.OpenSubKey(@"SOFTWARE\MyCompany\MySoftware")) { if (registryKey != null) { configurationDirectory = (string)registryKey.GetValue("ConfigurationDirectory"); } } } 
 try { RegistryKey regKey = Registry.LocalMachine; regKey = regKey.OpenSubKey(@"Software\Application\"); if (regKey != null) { return regKey.GetValue("KEY NAME").ToString(); } else { return null; } } catch (Exception ex) { return null; } 

你可以用这个:

 ///  /// To read a registry key. /// input: KeyName (string) /// output: value (string) ///  public string Read(string KeyName) { // Opening the registry key RegistryKey rk = baseRegistryKey ; // Open a subKey as read-only RegistryKey sk1 = rk.OpenSubKey(subKey); // If the RegistrySubKey doesn't exist -> (null) if ( sk1 == null ) { return null; } else { try { // If the RegistryKey exists I get its value // or null is returned. return (string)sk1.GetValue(KeyName.ToUpper()); } catch (Exception e) { // AAAAAAAAAAARGH, an error! ShowErrorMessage(e, "Reading registry " + KeyName.ToUpper()); return null; } } } 

有关更多信息,请访问此网站 。