2009-11-04 139 views
20

我有一个正在从bin文件夹导入dll的web应用程序。读取注册表项

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导入将无法正常工作...... 我怎样才能解决这个问题?

回答

43

读取注册表非常简单。 Microsoft.Win32命名空间有一个Registry静态类。读取来自HKLM节点的关键,代码:

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

如果节点是HKCU,你可以用CurrentUser取代LocalMachine

一旦你有RegistryKey对象,使用GetValue从注册表中获取值。继续使用上面的例子中,获得路径名注册表值是:

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

,当你用它做不要忘记关闭RegistryKey对象(或把语句获取价值为Using块)。

更新

我看到一对夫妇的事情。首先,我会改变路径名是一个静态属性定义为:

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

的两个问题是:

  1. RegistryKey参考将保持注册表打开。使用它作为类中的静态变量将导致计算机上出现问题。
  2. 注册表路径使用正斜杠,而不是反斜杠。
2
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; 
} 
1

您可以使用此:

/// <summary> 
/// To read a registry key. 
/// input: KeyName (string) 
/// output: value (string) 
/// </summary> 
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; 
     } 
    } 
} 

欲了解更多信息,请访问this web site

8

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

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

所有这些答案都可能导致运行在64位操作系统上的问题 - 这是通常的现在。

在我的情况下,我编译为'任何CPU'目标,当我安装在64位操作系统上时,软件工作正常。 但我的单元测试遇到了问题 - 显然它们是在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"); 
     } 
    } 
}