c# - 在注册表中查找应用程序路径

标签 c# .net

我需要计算机上已安装应用程序的列表及其路径目录,我找到了这个:

     //Registry path which has information of all the softwares installed on machine
    string uninstallKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
    using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(uninstallKey))
    {
        foreach (string skName in rk.GetSubKeyNames())
        {
            using (RegistryKey sk = rk.OpenSubKey(skName))
            {
                // we have many attributes other than these which are useful.
                Console.WriteLine(sk.GetValue("DisplayName") + 
            "  " + sk.GetValue("DisplayVersion"));
            }

        }
    }

我如何获得路径?我试过 sk.GetValue("DisplayPath") 但它不起作用。

谢谢!

最佳答案

每个软件都会有一个不同的 InstallLocation 名称,如果它存在的话。

永远是 UninstallString,它返回卸载程序 (exe) 的路径,您可以从中删除目录。

但是,您应该知道,如果您使用 HKEY_CURRENT_USER,您不会在那里获得所有已安装的程序。

您应该通过 HKEY_LOCAL_MACHINE。

因此,您正在寻找的代码是:

string uninstallExePath = sk.GetValue("UninstallString");
DirectoryInfo directory = new FileInfo(uninstallExePath).Directory;

string directoryPath = directory.FullName;

关于c# - 在注册表中查找应用程序路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10459977/

相关文章:

c# - 得到一个整数?从数据库使用 DbDataReader

c# - 在 WP7 应用程序中推出和读取基础数据

c# - 如何在 switch 语句中添加 "or"?

c# - 创建自定义 winforms 容器

c# - 如何在运行时更改当前资源管理器以切换语言?

c# - 是否可以在没有外键的情况下设置导航属性?

c# - JSON 嵌套数组

c# - Azure 表存储过期

c# - 我如何做一个简单的“更新 [table] where [any field] = [any value]

.net - Windows Phone 8 应用程序在 git 控制下无法在模拟器上运行