阅读注册表项

use*_*084 22 c# asp.net registry dllimport visual-studio

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

const string dllpath = "Utility.dll";

    [DllImport(dllpath)]
Run Code Online (Sandbox Code Playgroud)

现在我想要做的是首先从不在当前项目中但在某个不同位置的文件夹中导入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)
    {
Run Code Online (Sandbox Code Playgroud)

string pathName = (string)registryKey.GetValue("BinDir"); 是不是在这里工作,但正在页面加载事件...

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

Jef*_*ver 46

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

RegistryKey registryKey = Registry.LocalMachine.OpenSubKey("Software\\NodeName")
Run Code Online (Sandbox Code Playgroud)

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

一旦你的RegistryKey对象,使用GetValue来从注册表中的值.继续使用上面的示例,获取pathName注册表值将是:

string pathName = (string) registryKey.GetValue("pathName");
Run Code Online (Sandbox Code Playgroud)

并且不要忘记RegistryKey在完成对象时关闭对象(或者将语句放入Using块中以获取值).

更新

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

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

这两个问题是:

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


P.B*_*key 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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 7

所有这些答案都可能导致在 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)
Run Code Online (Sandbox Code Playgroud)

我们总共可以使用。

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");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)