23

我有一个从 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 导入将无法正常工作......我该如何解决这个问题?

4

5 回答 5

47

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

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

如果节点是HKCU,则可以替换LocalMachineCurrentUser

获得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. 注册表路径使用正斜杠,而不是反斜杠。
于 2009-11-04T19:04:09.890 回答
9

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

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
        }
    }
}
于 2013-03-25T20:19:21.160 回答
8

所有这些答案都可能导致在 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");
        }
    }
}
于 2017-05-08T08:27:24.160 回答
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;
}
于 2009-11-04T19:12:22.313 回答
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;
        }
    }
}

欲了解更多信息,请访问此网站

于 2013-02-05T15:39:10.353 回答