[英]Read a Registry Key
I have a web application which is importing DLLs from the bin folder. 我有一个Web应用程序,它从bin文件夹导入DLL。
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. 现在我想要做的是首先从不在当前项目中但在某个不同位置的文件夹中导入DLL。
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? 但是,如果我这样做DLL导入将无法正常工作......我该如何解决这个问题?
Reading the registry is pretty straightforward. 阅读注册表非常简单。 The
Microsoft.Win32
namespace has a Registry
static class. Microsoft.Win32
命名空间具有Registry
静态类。 To read a key from the HKLM
node, the code is: 要从
HKLM
节点读取密钥,代码为:
RegistryKey registryKey = Registry.LocalMachine.OpenSubKey("Software\\NodeName")
If the node is HKCU
, you can replace LocalMachine
with CurrentUser
. 如果节点是
HKCU
,则可以将LocalMachine
替换为CurrentUser
。
Once you have the RegistryKey
object, use GetValue
to get the value from the registry. 获得
RegistryKey
对象后,使用GetValue
从注册表中获取值。 Continuing Using the example above, getting the pathName registry value would be: 继续使用上面的示例,获取pathName注册表值将是:
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). 并且不要忘记在完成后关闭
RegistryKey
对象(或者将语句放入Using
块中)。
Updates 更新
I see a couple of things. 我看到了几件事。 First, I would change pathName to be a static property defined as:
首先,我将pathName更改为静态属性,定义为:
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. RegistryKey
引用将使注册表保持打开状态。 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
}
}
}
All these answers may lead to problems running on 64bit OS - which is usual nowadays. 所有这些答案都可能导致在64位操作系统上运行时出现问题 - 这在当今很常见。
In my situation, i compile to 'Any CPU' target and the software is working fine when i install on 64bit OS. 在我的情况下,我编译为'任何CPU'目标,当我在64位操作系统上安装时,软件工作正常。 But my unit tests are running into problems - obviously they are executed in 32bit mode.
但我的单元测试遇到了问题 - 显然它们是在32位模式下执行的。
In this case not the HKEY_LOCAL_MACHINE\\SOFTWARE\\MyCompany\\MySoftware
is searched but HKEY_LOCAL_MACHINE\\SOFTWARE\\WOW6432Node\\MyCompany\\MySoftware
but there are no entries! 在这种情况下,不会搜索
HKEY_LOCAL_MACHINE\\SOFTWARE\\MyCompany\\MySoftware
,但HKEY_LOCAL_MACHINE\\SOFTWARE\\WOW6432Node\\MyCompany\\MySoftware
但没有条目!
In this situation we have to specify the start point of our search using 在这种情况下,我们必须使用指定搜索的起点
RegistryKey hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)
In total we can use. 我们总共可以使用。
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;
}
You can use this: 你可以用这个:
/// <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;
}
}
}
For more information visit this web site . 有关更多信息,请访问此网站 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.