在 C# 中读取注册表项
我开发了一个应用程序并将其安装在客户端计算机上。在我的应用程序中,我需要获取其安装路径。我的应用程序有一个注册表项:
HKEY_LOCAL_MACHINE\SOFTWARE\MyApplication\[AppPath]
How can I read AppPath
using C#?
I have developed an application and installed it on a client computer. In my application I need to get its installation path. My application has a registry entry at:
HKEY_LOCAL_MACHINE\SOFTWARE\MyApplication\[AppPath]
How can I read AppPath
using C#?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您正在寻找名为
注册表。 GetValue
方法。You're looking for the cunningly named
Registry.GetValue
method.该代码应该得到你的价值。 不过,你需要让它
发挥作用。
That code should get your value. You'll need to be
for that to work though.
看到这个 http://www.codeproject .com/Articles/3389/Read-write-and-delete-from-registry-with-C
更新:
您可以使用
RegistryKey
类>Microsoft.Win32 命名空间。RegistryKey
的一些重要功能如下:see this http://www.codeproject.com/Articles/3389/Read-write-and-delete-from-registry-with-C
Updated:
You can use
RegistryKey
class underMicrosoft.Win32
namespace.Some important functions of
RegistryKey
are as follows:您可以使用以下内容来获取注册表认为其安装的位置:
或者您可以使用以下内容来找出应用程序实际从何处启动:
如果您尝试使用
.exe
位置作为相对路径来查找相关文件。用户可以在安装后轻松移动内容,并且应用程序仍然可以正常工作,因为 .NET 应用程序不太依赖于注册表。使用 StartupPath,您甚至可以做一些聪明的事情,例如让您的应用程序在运行时更新注册表项,而不是由于缺少/错误/损坏的条目而严重崩溃。
请务必将应用设置功能视为值存储而不是注册表 (
Properties.Settings.Default.mySettingEtc
)。您可以读取/写入应用程序和/或用户级别的设置,这些设置在标准位置中保存为简单的MyApp.exe.config
文件。过去(好的旧 Win 3.1/DOS 时代)让应用程序安装/删除是一个或两个文件夹结构的简单复制/删除,而不是一些复杂的、神秘的安装/卸载例程,留下各种垃圾,这是一个很好的爆炸在注册表中并散布在整个硬盘驱动器上。You can use the following to get where the registry thinks it's installed:
Or you can use the following to find out where the application is actually being launched from:
The latter is more reliable than the former if you're trying to use the
.exe
location as a relative path to find related files. The user could easily move things around after the install and still have the app work fine because .NET apps aren't so dependent upon the registry.Using
StartupPath
, you could even do something clever like have your app update the registry entries at run time instead of crashing miserably due to missing/wrong/corrupted entries.And be sure to look at the app settings functionality as storage for values rather than the registry (
Properties.Settings.Default.mySettingEtc
). You can read/write settings for the app and/or the user levels that get saved as simpleMyApp.exe.config
files in standard locations. A nice blast from the past (good old Win 3.1/DOS days) to have the application install/delete be a simple copy/delete of a folder structure or two rather than some convoluted, arcane install/uninstall routine that leaves all kinds of garbage in the registry and sprinkled all over the hard drive.如果您希望将其转换为特定类型,可以使用此方法。默认情况下,大多数非基本类型不支持直接转换,因此您必须相应地处理这些类型。
If you want it casted to a specific type you can use this method. Most non primitive types won't by default support direct casting so you will have to handle those accordingly.
使用 Microsoft.Win32;
using Microsoft.Win32;