65.9K
CodeProject 正在变化。 阅读更多。
Home

在 C# 中获取已安装应用程序列表

starIconstarIconstarIconstarIconstarIcon

5.00/5 (12投票s)

2014年6月6日

CPOL
viewsIcon

82002

downloadIcon

6165

如何在 C# 语言中列出系统上已安装的应用程序及其安装细节

引言

在本技巧中,我们将解释如何在 C# 语言中列出系统上已安装的应用程序及其安装细节。

背景

此技巧基于一个注册表键,其地址为 HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall,您可以在其中找到许多子键,这些子键可以帮助我们获取已安装应用程序的各种详细信息。

Using the Code

因此,为了完成我们想要做的事情,我们必须遵循以下步骤

  1. 在您的设计窗体中添加一个 listviewer 和一个按钮。
  2. 我们想要使用 "RegistryKey" 类,因此将 "using Microsoft.Win32" 添加到您的项目 using 语句中。
  3. 将此代码放在您的按钮点击事件中
    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
                            {
    
                                var displayName = sk.GetValue("DisplayName");
                                var size = sk.GetValue("EstimatedSize");
    
                                ListViewItem item;
                                if (displayName != null)
                                {
                                    if (size != null)
                                        item = new ListViewItem(new string[] {displayName.ToString(), 
                                                           size.ToString()});
                                    else
                                        item = new ListViewItem(new string[] { displayName.ToString() });
                                    lstDisplayHardware.Items.Add(item);
                                }
                            }
                            catch (Exception ex)
                            { }
                        }
                    }
                    label1.Text += " (" + lstDisplayHardware.Items.Count.ToString() + ")";
                }  

    说明:在此代码中,我们首先在特定地址(uninstallKey)中定义了一个注册表命令。然后,我们使用 DisplayName EstimatedSize 子键,这些子键向我们显示所有已安装应用程序的 Name Size ,最后,结果将在 listviewer 中显示。

  4. 您可以使用下面的列表中所有子键
    Value / Windows Installer property
    
    DisplayName ==> ProductName property
    DisplayVersion ==> Derived from ProductVersion property
    Publisher ==> Manufacturer property
    VersionMinor ==> Derived from ProductVersion property
    VersionMajor ==> Derived from ProductVersion property
    Version ==> Derived from ProductVersion property
    HelpLink ==> ARPHELPLINK property
    HelpTelephone ==> ARPHELPTELEPHONE property
    InstallDate ==> The last time this product received service. 
    The value of this property is replaced each time a patch is applied or removed from 
    the product or the /v Command-Line Option is used to repair the product. 
    If the product has received no repairs or patches this property contains 
    the time this product was installed on this computer.
    InstallLocation ==> ARPINSTALLLOCATION property
    InstallSource ==> SourceDir property
    URLInfoAbout ==> ARPURLINFOABOUT property
    URLUpdateInfo ==> ARPURLUPDATEINFO property
    AuthorizedCDFPrefix ==> ARPAUTHORIZEDCDFPREFIX property
    Comments ==> Comments provided to the Add or Remove Programs control panel.
    Contact ==> Contact provided to the Add or Remove Programs control panel.
    EstimatedSize ==> Determined and set by the Windows Installer.
    Language ==> ProductLanguage property
    ModifyPath ==> Determined and set by the Windows Installer.
    Readme ==> Readme provided to the Add or Remove Programs control panel.
    UninstallString ==> Determined and set by Windows Installer.
    SettingsIdentifier ==> MSIARPSETTINGSIDENTIFIER property

来源: http://msdn.microsoft.com/en-us/library/aa372105(v=vs.85).aspx

注意: 您也可以在代码中使用以下方法代替上述代码

try
    {
      string[] row = { sk.GetValue("DisplayName").ToString() , 
      sk.GetValue("EstematedSize").ToString()};
      var listViewItem = new ListViewItem(row);
      lstDisplayHardware.Items.Add(listViewItem);
    }

catch (Exception ex)
    { }

现在构建您的程序!

© . All rights reserved.