读取C#中的注册表项 [英] Read registry key in C#

查看:68
本文介绍了读取C#中的注册表项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在读取注册表项的值:

I'm reading the value of a registry key:

Microsoft.Win32.RegistryKey key;
key = *someLongPathHere*;

并在标签上显示值:

string a = (string)key.GetValue("");//a default value
label1.Text = a;

它显示:

{F241C880-6982-4CE5-8CF7-7085BA96DA5A}

除第一个下划线(该下划线存在于原始值中)外,大多数情况下都是正确的:

which is mostly correct, except for the first underscore, which exists in the original value:

{_F241C880-6982-4CE5-8CF7-7085BA96DA5A}

为什么会发生?即缺少下划线?

Why does it happen? i.e the missing underscore?

此外,在读取钥匙之后,我是否必须关闭钥匙或其他物品?我该怎么办?

Also, after reading the key, do I have to close the key or anything? How can I do it?

推荐答案

测试包含下划线的字符串值是否存在系统性问题很容易.您可以简单地在注册表编辑器中创建这样的值,然后使用 GetValue()将其读入C#程序.这样做时,您会发现C#注册表代码不会丢失下划线.因此,对于您的问题,必须有其他解释.

It's easy enough to test whether or not there is a systematic problem with string values containing underscores. You can simply create such a value in the registry editor and then read it into your C# program with GetValue(). When you do so you'll discover that the C# registry code doesn't lose underscores. So, there must be some other explanation for your problem.

我的最佳猜测是您的标签组件未显示下划线.我对C#UI框架不是很熟悉,但这似乎是合理的.尝试在调试器下而不是在UI的标签标题中查看 a 的值.

My best guess is that your label component does not display the underscore. I'm not very familiar with the C# UI frameworks but that seems plausible. Try looking at the value of a under the debugger rather than in a label caption on your UI.

想到的另一件事是,您具有注册表重定向,因为您有一个在x64上运行的x86进程,并且您的密钥位于重定向的密钥下,例如HKLM \ Software.也许如果您在Wow6432Node下查看,您将看到下划线的差异.

The other thing that comes to mind is that you have registry redirection because you have an x86 process running on x64, and your key is under a redirected key, HKLM\Software, for example. Perhaps if you look under the Wow6432Node you will see the underscore discrepancy.

对于管理密钥的寿命,密钥由未管理的资源支持.即Windows HKEY . RegistryKey 类实现了 IDisposable ,因此您应该将密钥包装在 using 语句中.

As for managing the life of the key, the key is backed by an unmanged resource. Namely a Windows HKEY. The RegistryKey class implements IDisposable and so you should wrap your keys in a using statement.

这篇关于读取C#中的注册表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆