在 C# 中读取注册表项

发布于 2024-10-08 05:18:34 字数 237 浏览 0 评论 0原文

我开发了一个应用程序并将其安装在客户端计算机上。在我的应用程序中,我需要获取其安装路径。我的应用程序有一个注册表项:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

梦初启 2024-10-15 05:18:34

您正在寻找名为 注册表。 GetValue方法

You're looking for the cunningly named Registry.GetValue method.

小嗷兮 2024-10-15 05:18:34
string InstallPath = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\MyApplication\AppPath", "Installed", null);    
if (InstallPath != null)
{
    // Do stuff
}

该代码应该得到你的价值。 不过,你需要让它

using Microsoft.Win32;

发挥作用。

string InstallPath = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\MyApplication\AppPath", "Installed", null);    
if (InstallPath != null)
{
    // Do stuff
}

That code should get your value. You'll need to be

using Microsoft.Win32;

for that to work though.

冷心人i 2024-10-15 05:18:34

看到这个 http://www.codeproject .com/Articles/3389/Read-write-and-delete-from-registry-with-C

更新:

您可以使用 RegistryKey 类>Microsoft.Win32 命名空间。

RegistryKey的一些重要功能如下:

GetValue       //to get value of a key
SetValue       //to set value to a key
DeleteValue    //to delete value of a key
OpenSubKey     //to read value of a subkey (read-only)
CreateSubKey   //to create new or edit value to a subkey
DeleteSubKey   //to delete a subkey
GetValueKind   //to retrieve the datatype of registry key

see this http://www.codeproject.com/Articles/3389/Read-write-and-delete-from-registry-with-C

Updated:

You can use RegistryKey class under Microsoft.Win32 namespace.

Some important functions of RegistryKey are as follows:

GetValue       //to get value of a key
SetValue       //to set value to a key
DeleteValue    //to delete value of a key
OpenSubKey     //to read value of a subkey (read-only)
CreateSubKey   //to create new or edit value to a subkey
DeleteSubKey   //to delete a subkey
GetValueKind   //to retrieve the datatype of registry key
暖树树初阳… 2024-10-15 05:18:34

您可以使用以下内容来获取注册表认为其安装的位置:

(string)Registry.LocalMachine.GetValue(@"SOFTWARE\MyApplication\AppPath",
   "Installed", null);

或者您可以使用以下内容来找出应用程序实际从何处启动:

System.Windows.Forms.Application.StartupPath

如果您尝试使用 .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:

(string)Registry.LocalMachine.GetValue(@"SOFTWARE\MyApplication\AppPath",
   "Installed", null);

Or you can use the following to find out where the application is actually being launched from:

System.Windows.Forms.Application.StartupPath

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 simple MyApp.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.

晨敛清荷 2024-10-15 05:18:34

如果您希望将其转换为特定类型,可以使用此方法。默认情况下,大多数非基本类型不支持直接转换,因此您必须相应地处理这些类型。

  public T GetValue<T>(string registryKeyPath, string value, T defaultValue = default(T))
  {
    T retVal = default(T);

      retVal = (T)Registry.GetValue(registryKeyPath, value, defaultValue);

      return retVal;
  }

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.

  public T GetValue<T>(string registryKeyPath, string value, T defaultValue = default(T))
  {
    T retVal = default(T);

      retVal = (T)Registry.GetValue(registryKeyPath, value, defaultValue);

      return retVal;
  }
旧城空念 2024-10-15 05:18:34

使用 Microsoft.Win32;

  string chkRegVC = "NO";
   private void checkReg_vcredist() {

        string regKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
        using (Microsoft.Win32.RegistryKey uninstallKey = Registry.LocalMachine.OpenSubKey(regKey))
        {
            if (uninstallKey != null)
            {
                string[] productKeys = uninstallKey.GetSubKeyNames();
                foreach (var keyName in productKeys)
                {

                    if (keyName == "{196BB40D-1578-3D01-B289-BEFC77A11A1E}" ||//Visual C++ 2010 Redistributable Package (x86) 
                        keyName == "{DA5E371C-6333-3D8A-93A4-6FD5B20BCC6E}" ||//Visual C++ 2010 Redistributable Package (x64) 
                        keyName == "{C1A35166-4301-38E9-BA67-02823AD72A1B}" ||//Visual C++ 2010 Redistributable Package (ia64) 
                        keyName == "{F0C3E5D1-1ADE-321E-8167-68EF0DE699A5}" ||//Visual C++ 2010 SP1 Redistributable Package (x86) 
                        keyName == "{1D8E6291-B0D5-35EC-8441-6616F567A0F7}" ||//Visual C++ 2010 SP1 Redistributable Package (x64) 
                        keyName == "{88C73C1C-2DE5-3B01-AFB8-B46EF4AB41CD}"   //Visual C++ 2010 SP1 Redistributable Package (ia64) 
                        ) { chkRegVC = "OK"; break; }
                    else { chkRegVC = "NO"; }
                }
            }
        }
    }

using Microsoft.Win32;

  string chkRegVC = "NO";
   private void checkReg_vcredist() {

        string regKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
        using (Microsoft.Win32.RegistryKey uninstallKey = Registry.LocalMachine.OpenSubKey(regKey))
        {
            if (uninstallKey != null)
            {
                string[] productKeys = uninstallKey.GetSubKeyNames();
                foreach (var keyName in productKeys)
                {

                    if (keyName == "{196BB40D-1578-3D01-B289-BEFC77A11A1E}" ||//Visual C++ 2010 Redistributable Package (x86) 
                        keyName == "{DA5E371C-6333-3D8A-93A4-6FD5B20BCC6E}" ||//Visual C++ 2010 Redistributable Package (x64) 
                        keyName == "{C1A35166-4301-38E9-BA67-02823AD72A1B}" ||//Visual C++ 2010 Redistributable Package (ia64) 
                        keyName == "{F0C3E5D1-1ADE-321E-8167-68EF0DE699A5}" ||//Visual C++ 2010 SP1 Redistributable Package (x86) 
                        keyName == "{1D8E6291-B0D5-35EC-8441-6616F567A0F7}" ||//Visual C++ 2010 SP1 Redistributable Package (x64) 
                        keyName == "{88C73C1C-2DE5-3B01-AFB8-B46EF4AB41CD}"   //Visual C++ 2010 SP1 Redistributable Package (ia64) 
                        ) { chkRegVC = "OK"; break; }
                    else { chkRegVC = "NO"; }
                }
            }
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文