Use C# to get system installed applications
Question:
How to determine the applications installed on the system using C# code?
Solution:
To get a list of installed applications on your system using C#, you can use the registry or WMI (Windows Management Instrumentation).
Registry-based method:
Iterating through the registry key "SOFTWAREMicrosoftWindowsCurrentVersionUninstall" can provide a complete list of installed applications. Each subkey represents a different application, and the "DisplayName" value contains the name of the application.
WMI method:
A slower but feasible alternative is to use WMI through the ManagementObjectSearcher object. The query "SELECT * FROM Win32_Product" will retrieve a list of installed programs. The "Name" property of each ManagementObject represents the name of the application.
Code snippet:
Based on registry:
<code class="language-csharp">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")); } } }</code>
Based on WMI:
<code class="language-csharp">ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT * FROM Win32_Product"); foreach (ManagementObject mo in mos.Get()) { Console.WriteLine(mo["Name"]); }</code>
Note:
Registry-based methods may also capture Windows updates and components. WMI, although slower, may miss installed applications for all users. Depending on your needs, you can choose the appropriate method.
The above is the detailed content of How Can I Retrieve a List of Installed Applications in C#?. For more information, please follow other related articles on the PHP Chinese website!