2010-08-19 106 views
4

如何获取系统上安装的软件产品列表。我的目标是遍历这些,并获得其中一些的安装路径。如何获取已安装软件产品的列表?

伪代码(结合多国语言:))

foreach InstalledSoftwareProduct 
    if InstalledSoftwareProduct.DisplayName LIKE *Visual Studio* 
     print InstalledSoftwareProduct.Path 
+1

我严重怀疑,你可以在Windows系统中最简单的方法。大多数程序都提供他们自己的安装程序,而没有关于如何检测软件是否安装的标准。 – alternative 2010-08-19 21:40:45

+0

@mathepic:实际上绝大多数程序都提供了一个安装程序,它是通过.msi安装(又称Windows安装程序)安装的一个或另一个安装程序。 – 2010-08-19 21:51:08

回答

11

您可以使用MSI API函数来枚举所有已安装的产品。下面你会找到这样的示例代码。

在我的代码中,我首先枚举所有产品,获取产品名称,如果它包含字符串“Visual Studio”,则检查InstallLocation属性。但是,此属性并不总是设置。我不确定这是不是要检查的正确属性,还是有另一个属性始终包含目标目录。也许从InstallLocation属性中检索到的信息对您来说已经足够了?

using System; 
using System.Collections.Generic; 
using System.Runtime.InteropServices; 
using System.Text; 

class Program 
{ 
    [DllImport("msi.dll", CharSet = CharSet.Unicode)] 
    static extern Int32 MsiGetProductInfo(string product, string property, 
     [Out] StringBuilder valueBuf, ref Int32 len); 

    [DllImport("msi.dll", SetLastError = true)] 
    static extern int MsiEnumProducts(int iProductIndex, 
     StringBuilder lpProductBuf); 

    static void Main(string[] args) 
    { 
     StringBuilder sbProductCode = new StringBuilder(39); 
     int iIdx = 0; 
     while (
      0 == MsiEnumProducts(iIdx++, sbProductCode)) 
     { 
      Int32 productNameLen = 512; 
      StringBuilder sbProductName = new StringBuilder(productNameLen); 

      MsiGetProductInfo(sbProductCode.ToString(), 
       "ProductName", sbProductName, ref productNameLen); 

      if (sbProductName.ToString().Contains("Visual Studio")) 
      { 
       Int32 installDirLen = 1024; 
       StringBuilder sbInstallDir = new StringBuilder(installDirLen); 

       MsiGetProductInfo(sbProductCode.ToString(), 
        "InstallLocation", sbInstallDir, ref installDirLen); 

       Console.WriteLine("ProductName {0}: {1}", 
        sbProductName, sbInstallDir); 
      } 
     } 
    } 
} 
0

你需要存储其上,你可以使用类似注册表安装路径,那么,如果所有的节目: http://visualbasic.about.com/od/quicktips/qt/regprogpath.htm(我知道这是VB,但同样的原则) 。

我敢肯定,如果有一些不存储他们的安装路径(或者晦涩地执行),我确定有可能通过.NET获取程序列表,但我不知道。

8

您可以问WMI Installed applications classesWin32_Products类代表Windows安装程序安装的所有产品。例如下面的PS脚本将检索安装在本地计算机上的所有农资由Windows安装程序安装的:

Get-WmiObject -Class Win32_Product -ComputerName . 

Working with Software Installations。将PS查询指向等效的C#使用WMI API(换句话说Using WMI with the .NET Framework)作为练习留给读者。

+0

Awesome Powershell命令!我很高兴编写了一个脚本,用来比较计算机A和B上安装的内容,但不是C和D(第一组中的某些内容,但不是第二组内容))。 – 2010-10-19 18:42:22

+1

请注意使用Win32_Product,因为这里记录了一些令人讨厌的副作用[link](http://sdmsoftware.com/wmi/why-win32_product-is-bad-news/) – 2013-03-19 17:17:10

+0

@BobM:http://support.microsoft .com/kb/974524的确如此。 – 2013-03-19 21:04:25

0

通过注册表

using Microsoft.Win32; 
using System; 
using System.Collections.Generic; 
using System.Text; 
using System.IO; 


namespace SoftwareInventory 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      //!!!!! Must be launched with a domain administrator user!!!!! 
      Console.ForegroundColor = ConsoleColor.Green; 
      StringBuilder sbOutFile = new StringBuilder(); 
      Console.WriteLine("DisplayName;IdentifyingNumber"); 
      sbOutFile.AppendLine("Machine;DisplayName;Version"); 

      //Retrieve machine name from the file :File_In/collectionMachines.txt 
      //string[] lines = new string[] { "NameMachine" }; 
      string[] lines = File.ReadAllLines(@"File_In/collectionMachines.txt"); 
      foreach (var machine in lines) 
      { 
       //Retrieve the list of installed programs for each extrapolated machine name 
       var registry_key = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"; 
       using (Microsoft.Win32.RegistryKey key = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, machine).OpenSubKey(registry_key)) 
       { 
        foreach (string subkey_name in key.GetSubKeyNames()) 
        { 
         using (RegistryKey subkey = key.OpenSubKey(subkey_name)) 
         { 
          //Console.WriteLine(subkey.GetValue("DisplayName")); 
          //Console.WriteLine(subkey.GetValue("IdentifyingNumber")); 
          if (subkey.GetValue("DisplayName") != null && subkey.GetValue("DisplayName").ToString().Contains("Visual Studio")) 
          { 
           Console.WriteLine(string.Format("{0};{1};{2}", machine, subkey.GetValue("DisplayName"), subkey.GetValue("Version"))); 
           sbOutFile.AppendLine(string.Format("{0};{1};{2}", machine, subkey.GetValue("DisplayName"), subkey.GetValue("Version"))); 
          } 
         } 
        } 
       } 
      } 
      //CSV file creation 
      var fileOutName = string.Format(@"File_Out\{0}_{1}.csv", "Software_Inventory", DateTime.Now.ToString("yyyy_MM_dd_HH_mmssfff")); 
      using (var file = new System.IO.StreamWriter(fileOutName)) 
      { 

       file.WriteLine(sbOutFile.ToString()); 
      } 
      //Press enter to continue 
      Console.WriteLine("Press enter to continue !"); 
      Console.ReadLine(); 
     } 


    } 
} 
相关问题