遍历注册表项 [英] Iterate through registry entries

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

问题描述

的建议 href=\"http://stackoverflow.com/questions/1458483/how-to-check-with-c-where-a-program-is-installed\">,我需要通过条目

As suggested here, I need to iterate through entries in

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\

要找出我的应用程序的安装路径。如何循环,这样我可以找出
INSTALLLOCATION 给定值在显示名称。如何有效地做到这一点在C#

to find out the installed path of my application. How to iterate so that I can find out the InstallLocation value given the DisplayName. How to do it efficiently in C#.

推荐答案

下面的代码来实现你的目标:

Below is code to achieve your goal:

class Program
{
    static void Main(string[] args)
    {
        RegistryKey key = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall");
        foreach (var v in key.GetSubKeyNames())
        {
            Console.WriteLine(v);

            RegistryKey productKey = key.OpenSubKey(v);
            if (productKey != null)
            {
                foreach (var value in productKey.GetValueNames())
                {
                    Console.WriteLine("\tValue:" + value);

                    // Check for the publisher to ensure it's our product
                    string keyValue = Convert.ToString(productKey.GetValue("Publisher"));
                    if (!keyValue.Equals("MyPublisherCompanyName", StringComparison.OrdinalIgnoreCase))
                        continue;

                    string productName = Convert.ToString(productKey.GetValue("DisplayName"));
                    if (!productName.Equals("MyProductName", StringComparison.OrdinalIgnoreCase))
                        return;

                    string uninstallPath = Convert.ToString(productKey.GetValue("InstallSource"));

                    // Do something with this valuable information
                }
            }
        }

        Console.ReadLine();
    }
}

修改查看< A HREF =http://stackoverflow.com/a/26686738/495455>这种方法更全面的方法来找到一个应用程序安装路径,它展示了使用配置作为意见提出。
http://stackoverflow.com/a/26686738/495455

See this method for a more comprehensive way to find an Applications Install Path, it demonstrates the using disposing as suggested in the comments. http://stackoverflow.com/a/26686738/495455

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

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