在系统中获取已安装的应用程序

问题描述 投票:71回答:10

如何使用c#代码在系统中安装应用程序?

c# .net installer installation
10个回答
99
投票

通过注册表项“SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Uninstall”迭代似乎提供了已安装应用程序的完整列表。

除了下面的例子,你可以找到与我所做的here类似的版本。

这是一个粗略的例子,你可能想要做一些事情来删除像提供的第二个链接中的空白行。

string registry_key = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
using(Microsoft.Win32.RegistryKey key = Registry.LocalMachine.OpenSubKey(registry_key))
{
    foreach(string subkey_name in key.GetSubKeyNames())
    {
        using(RegistryKey subkey = key.OpenSubKey(subkey_name))
        {
            Console.WriteLine(subkey.GetValue("DisplayName"));
        }
    }
}

或者,您可以使用WMI,如上所述:

ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT * FROM Win32_Product");
foreach(ManagementObject mo in mos.Get())
{
    Console.WriteLine(mo["Name"]);
}

但执行速度相当慢,我听说它可能只列出“ALLUSERS”下安装的程序,尽管这可能不正确。它还忽略了Windows组件和更新,这对您来说可能很方便。


0
投票

我的要求是检查我的系统中是否安装了特定的软件。此解决方案按预期工作。它可能对你有帮助。我使用Visual Studio 2015中的c#中的windows应用程序。

 private void Form1_Load(object sender, EventArgs e)
        {

            object line;
            string softwareinstallpath = string.Empty;
            string registry_key = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
            using (var baseKey = Microsoft.Win32.RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
            {
                using (var key = baseKey.OpenSubKey(registry_key))
                {
                    foreach (string subkey_name in key.GetSubKeyNames())
                    {
                        using (var subKey = key.OpenSubKey(subkey_name))
                        {
                            line = subKey.GetValue("DisplayName");
                            if (line != null && (line.ToString().ToUpper().Contains("SPARK")))
                            {

                                softwareinstallpath = subKey.GetValue("InstallLocation").ToString();
                                listBox1.Items.Add(subKey.GetValue("InstallLocation"));
                                break;
                            }
                        }
                    }
                }
            }

            if(softwareinstallpath.Equals(string.Empty))
            {
                MessageBox.Show("The Mirth connect software not installed in this system.")
            }



            string targetPath = softwareinstallpath + @"\custom-lib\";
            string[] files = System.IO.Directory.GetFiles(@"D:\BaseFiles");

            // Copy the files and overwrite destination files if they already exist. 
            foreach (var item in files)
            {
                string srcfilepath = item;
                string fileName = System.IO.Path.GetFileName(item);
                System.IO.File.Copy(srcfilepath, targetPath + fileName, true);
            }
            return;

        }

10
投票

你可以看看this article。它使用注册表来读取已安装的应用程序列表。

public void GetInstalledApps()
{
    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))
            {
                try
                {
                    lstInstalled.Items.Add(sk.GetValue("DisplayName"));
                }
                catch (Exception ex)
                { }
            }
        }
    }
}

8
投票

我同意通过注册表项枚举是最好的方法。

但请注意,给定的密钥@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"将列出32位Windows安装中的所有应用程序以及Windows 64位安装中的64位应用程序。

为了还能看到在Windows 64位安装上安装的32位应用程序,您还需要枚举密钥@"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall"


5
投票

值得注意的是,Win32_Product WMI类代表Windows Installer安装的产品。不是每个应用程序都使用Windows安装

但是“SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Uninstall”表示32位应用程序。对于64位,您还需要遍历“HKEY_LOCAL_MACHINE \ SOFTWARE \ Wow6432Node \ Microsoft \ Windows \ CurrentVersion \ Uninstall”,并且由于并非每个软件都具有64位版本,因此安装的总应用程序是两个位置上具有“UninstallString”的键的并集值得他们。

但最好的选项仍然是相同的.traverse注册表项是一种更好的方法,因为每个应用程序在注册表中都有一个条目[包括Windows Installer中的条目]。但是注册表方法是不安全的,好像有人删除了相应的密钥然后你就不会知道了应用程序条目。相反,更改HKEY_Classes_ROOT \安装程序更加棘手,因为它与Microsoft办公室或其他产品等许可问题相关联。对于更强大的解决方案,您始终可以将注册表替代与WMI结合使用。


1
投票

通过“HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Uninstall”键迭代并检查其“DisplayName”值。


1
投票

使用Windows Installer API!

它允许对所有程序进行可靠的枚举。注册表不可靠,但WMI是重量级的。


0
投票

你最好的选择是使用WMI。特别是Win32_Product类。


0
投票

我建议你看看WMI(Windows Management Instrumentation)。如果将System.Management引用添加到C#项目,您将获得对“ManagementObjectSearcher”类的访问权限,您可能会发现它很有用。

Installed Applications有各种WMI类,但如果它与Windows Installer一起安装,那么Win32_Product类可能最适合您。

ManagementObjectSearcher s = new ManagementObjectSearcher("SELECT * FROM Win32_Product");

0
投票

我使用了Nicks方法 - 我需要检查是否安装了Visual Studio for Visual Studio,它看起来有点慢,但是在一个单独的线程中这对我来说很好。 - 这是我的扩展代码:

    private bool isRdInstalled() {
        ManagementObjectSearcher p = new ManagementObjectSearcher("SELECT * FROM Win32_Product");
        foreach (ManagementObject program in p.Get()) {
            if (program != null && program.GetPropertyValue("Name") != null && program.GetPropertyValue("Name").ToString().Contains("Microsoft Visual Studio 2012 Remote Debugger")) {
                return true;
            }
            if (program != null && program.GetPropertyValue("Name") != null) {
                Trace.WriteLine(program.GetPropertyValue("Name"));
            }
        }
        return false;
    }
© www.soinside.com 2019 - 2024. All rights reserved.