How to get a list of installed software products?

How to get a list of software products that are installed in the system. My goal is to iterate over them and get a way to install several of them.

PSEUDOCODE (combining several languages ​​:))

foreach InstalledSoftwareProduct if InstalledSoftwareProduct.DisplayName LIKE *Visual Studio* print InstalledSoftwareProduct.Path 
+4
source share
4 answers

You can use the MSI api functions to list all installed products. Below you will find sample code that does this.

In my code, I first list all the products, get the product name and, if it contains the string "Visual Studio", I check the InstallLocation property. However, this property is not always set. I don’t know for sure if this is the wrong property to check or if there is another property that always contains the target directory. Maybe the information obtained from the InstallLocation property is sufficient for you?

 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); } } } } 
+11
source

You can specify WMI Installed application classes : The Win32_Products class represents all products installed by Windows Installer. For example, the following PS script will retrieve all prodcuts installed on the local computer that were installed by the Windows installer:

 Get-WmiObject -Class Win32_Product -ComputerName . 

See Working with software settings . Making a PS request for equivalent use of the C # WMI API (in other words, Using WMI with the .NET Framework ) remains as an exercise for the reader.

+8
source

Well, if all the programs you need store installation paths in the registry, you can use something like: http://visualbasic.about.com/od/quicktips/qt/regprogpath.htm (I know this is VB, but same principle).

I am sure that there may be a way to get a list of programs through .NET if some of them do not store the installation paths (or do it unclear), but I do not know this.

0
source

The simplest methods through the registry

 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(); } } } 
0
source

All Articles