c# - 遍历注册表项

标签 c#

按照建议here ,我需要遍历

中的条目
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\

找出我的应用程序的安装路径。如何迭代以便我可以找出 给定 DisplayNameInstallLocation 值。如何在 C# 中高效地执行此操作。

最佳答案

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

using Microsoft.Win32;
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();
    }
}

编辑:参见this method for a more comprehensive way to find an Applications Install Path ,它演示了如何按照注释中的建议处理 usinghttps://stackoverflow.com/a/26686738/495455

关于c# - 遍历注册表项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1458653/

相关文章:

c# - 如何在代码中创建一个 SuperSocket WebSocket 服务器

c# - 如何在 C# 中向枚举值添加描述以与 ASP.NET MVC 中的下拉列表一起使用?

c# - 从数据库读取并插入到 DataGridView 中

c# - 你如何使用 UTF8 编码 mysqldump 特定表?

c# - 反射和泛型获取属性值

c# - 洋葱架构 : Should we allow data annotations in our domain entities?

c# - 通用扩展方法 : Type argument cannot be inferred from the usage

c# - StackExchange.Redis 异步调用挂起

c# - Join 和 WaitAll 的比较

c# - 2D 游戏引擎中应该包含什么?