在网上查了很久,看到有很多通过注册表获取已安装软件列表,但是我试了下获取的都不全。
找到了一个使用VB写的程序是可以获取全部安装软件列表的,那个文章我找不到了,回头找到再贴出来。我这边用C#实现了他的代码的功能,也是可以获取所有软件的。
using Microsoft.Win32;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using UnityEngine;
public class InstalledProgram
{
public InstalledProgram(string ProgramDisplayName)
{
this.DisplayName = ProgramDisplayName;
}
public InstalledProgram(string ProgramDisplayName, string ProgramParentDisplayName , bool IsProgramUpdate, string ProgramVersion,string ProgramInstallLocation)
{
this.DisplayName = ProgramDisplayName;
this.ParentDisplayName = ProgramParentDisplayName;
this.IsUpdate = IsProgramUpdate;
this.Version = ProgramVersion;
this.InstallLocation = ProgramInstallLocation;
}
private string _DisplayName = string.Empty;
/// <summary>
/// The name that would be displayed in Add/Remove Programs
/// </summary>
public string DisplayName
{
get
{
return _DisplayName;
}
set
{
_DisplayName = value;
}
}
private string _Version = string.Empty;
/// <summary>
/// The version of the program
/// </summary>
public string Version
{
get
{
return _Version;
}
set
{
_Version = value;
}
}
private string _ParentDisplayName = string.Empty;
/// <summary>
/// If this program is an update then this will contain the display name of the product that this is an update to
/// </summary>
public string ParentDisplayName
{
get
{
return _ParentDisplayName;
}
set
{
_ParentDisplayName = value;
}
}
private bool _IsUpdate;
/// <summary>
/// Is this program classed as an update
/// </summary>
public bool IsUpdate
{
get
{
return _IsUpdate;
}
set
{
_IsUpdate = value;
}
}
private string _InstallLocation = string.Empty;
/// <summary>
/// 安装位置
/// </summary>
public string InstallLocation {
get
{
return _InstallLocation;
}
set
{
_InstallLocation = value;
}
}
}
public class GetWindowSoft : MonoBehaviour
{
private void Start()
{
GetAllInstalledProgram();
}
/// <summary>
/// 获取电脑安装的所有程序
/// </summary>
public void GetAllInstalledProgram()
{
List<InstalledProgram> ProgramList = new List<InstalledProgram>();
RegistryKey ClassesKey = Registry.LocalMachine.OpenSubKey(@"Software\Classes\Installer\Products");
//---Wow64 Uninstall key
RegistryKey Wow64UninstallKey = Registry.LocalMachine.OpenSubKey(@"Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall");
ProgramList = GetUninstallKeyPrograms(Wow64UninstallKey, ClassesKey, ProgramList, true);
//---Standard Uninstall key
RegistryKey StdUninstallKey = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall");
ProgramList = GetUninstallKeyPrograms(StdUninstallKey, ClassesKey, ProgramList, true);
foreach (string UserSid in Registry.Users.GetSubKeyNames())
{
//---HKU Uninstall key
RegistryKey CuUnInstallKey = Registry.Users.OpenSubKey(UserSid + @"\Software\Microsoft\Windows\CurrentVersion\Uninstall");
ProgramList = GetUninstallKeyPrograms(CuUnInstallKey, ClassesKey, ProgramList, true);
//---HKU Installer key
RegistryKey CuInstallerKey = Registry.Users.OpenSubKey(UserSid + @"\Software\Microsoft\Installer\Products");
ProgramList = GetUserInstallerKeyPrograms(CuInstallerKey, Registry.LocalMachine, ProgramList);
}
foreach (var item in ProgramList)
{
print(item.DisplayName+" -- "+item.InstallLocation);
}
try
{
Registry.Users.Close();
Registry.LocalMachine.Close();
}
catch (Exception)
{
throw;
}
}
List<InstalledProgram> GetUninstallKeyPrograms(RegistryKey UninstallKey, RegistryKey ClassesKey, List<InstalledProgram> ExistingProgramList, bool IncludeUpdates)
{
if (UninstallKey != null)
{
//'Loop through all subkeys (each one represents an installed program)
foreach (string SubKeyName in UninstallKey.GetSubKeyNames())
{
try
{
RegistryKey CurrentSubKey = UninstallKey.OpenSubKey(SubKeyName);
//'Skip this program if the SystemComponent flag is set
int IsSystemComponent = 0;
try
{
IsSystemComponent = (int)(CurrentSubKey.GetValue("SystemComponent", 0));
}
catch (Exception)
{
throw;
}
if (IsSystemComponent != 1)
{
//If the WindowsInstaller flag is set then add the key name to our list of Windows Installer GUIDs
if ((int)(CurrentSubKey.GetValue("WindowsInstaller", 0)) != 1)
{
// Regex WindowsUpdateRegEx = new Regex();
string ProgramReleaseType = (CurrentSubKey.GetValue("ReleaseType", String.Empty)).ToString();
//print("ProgramReleaseType:"+ProgramReleaseType);
string ProgVersion = string.Empty;
try
{
ProgVersion = (CurrentSubKey.GetValue("DisplayVersion", String.Empty)).ToString();
}
catch (Exception)
{
throw;
}
//Check to see if this program is classed as an update
if (Regex.Match(SubKeyName, "KB[0-9]{6}$").Success ||
(CurrentSubKey.GetValue("ParentKeyName", String.Empty)).ToString() != string.Empty ||
(ProgramReleaseType == "Security Update") ||
(ProgramReleaseType == "Update Rollup") ||
(ProgramReleaseType == "Hotfix"))
{
if (IncludeUpdates)
{
string Name = CurrentSubKey.GetValue("DisplayName", String.Empty).ToString();
if (Name != string.Empty && !IsProgramInList(Name, ExistingProgramList))
{
ExistingProgramList.Add(new InstalledProgram(Name, CurrentSubKey.GetValue("ParentDisplayName", String.Empty).ToString(), true, ProgVersion, GetInstallLocation(CurrentSubKey)));
}
}
}
else
{
bool UninstallStringExists = false;
foreach (string valuename in CurrentSubKey.GetValueNames())
{
if (string.Equals("UninstallString", valuename, StringComparison.CurrentCultureIgnoreCase))
{
UninstallStringExists = true;
}
}
if (UninstallStringExists)
{
string Name = CurrentSubKey.GetValue("DisplayName", String.Empty).ToString();
if (Name != string.Empty && !IsProgramInList(Name, ExistingProgramList))
{
ExistingProgramList.Add(new InstalledProgram(Name, CurrentSubKey.GetValue("ParentDisplayName", String.Empty).ToString(), false, ProgVersion, GetInstallLocation(CurrentSubKey)));
}
}
}
}
else//If WindowsInstaller
{
string ProgVersion = string.Empty;
string Name = string.Empty;
bool FoundName = false;
try
{
string MsiKeyName = GetInstallerKeyNameFromGuid(SubKeyName);
RegistryKey CrGuidKey = ClassesKey.OpenSubKey(MsiKeyName);
if (CrGuidKey != null)
{
Name = CrGuidKey.GetValue("ProductName", String.Empty).ToString();
}
}
catch (Exception) { }
try
{
ProgVersion = (CurrentSubKey.GetValue("DisplayVersion", String.Empty)).ToString();
}
catch (Exception)
{
throw;
}
if (Name != string.Empty && !IsProgramInList(Name, ExistingProgramList))
{
ExistingProgramList.Add(new InstalledProgram(Name, CurrentSubKey.GetValue("ParentDisplayName", String.Empty).ToString(), true, ProgVersion, GetInstallLocation(CurrentSubKey)));
}
}
}
}
catch (Exception) {
throw;
}
}
}
return ExistingProgramList;
}
/// <summary>
/// 获取软件打开路径(不是完全正确的)
/// </summary>
/// <param name="registryKey"></param>
/// <returns></returns>
string GetInstallLocation( RegistryKey registryKey)
{
string location = string.Empty;
string DisplayIcon = registryKey.GetValue("DisplayIcon", string.Empty).ToString();
string DisplayName = registryKey.GetValue("DisplayName", string.Empty).ToString();
string InstallLocation = registryKey.GetValue("InstallLocation", string.Empty).ToString();
string InstallSource = registryKey.GetValue("InstallSource", string.Empty).ToString();
if (DisplayIcon.Contains(".exe"))
{
string[] loc = DisplayIcon.Split(',');//去掉后面的“,0”
location= loc[0];
}
else if (InstallLocation != string.Empty)
{
location = InstallLocation + @"\" + DisplayName + ".exe";
}
if (InstallSource != string.Empty)
{
location = InstallSource+ DisplayName + ".exe";
}
return location;
}
bool IsProgramInList(string ProgramName, List<InstalledProgram> ListToCheck)
{
return ListToCheck.Contains(new InstalledProgram(ProgramName));
}
string GetInstallerKeyNameFromGuid(string GuidName)
{
string[] MsiNameParts = GuidName.Replace("{", "").Replace("}", "").Split('-');
StringBuilder MsiName = new StringBuilder();
//'Just reverse the first 3 parts
for (int i = 0; i <= 2; i++)
{
MsiName.Append(ReverseString(MsiNameParts[i]));
}
//For the last 2 parts, reverse each character pair
for (int j = 3; j <= 4; j++)
{
for (int i = 0; i < MsiNameParts[j].Length; i++)
{
MsiName.Append(MsiNameParts[j][i + 1]);
MsiName.Append(MsiNameParts[j][i]);
i += 1;
}
}
return MsiName.ToString();
}
string ReverseString(string input)
{
char[] chars = input.ToCharArray();
Array.Reverse(chars);
return chars.ToString();
}
List<InstalledProgram> GetUserInstallerKeyPrograms(RegistryKey CuInstallerKey, RegistryKey HklmRootKey, List<InstalledProgram> ExistingProgramList)
{
if (CuInstallerKey != null)
{
foreach (string CuProductGuid in CuInstallerKey.GetSubKeyNames())
{
bool ProductFound = false;
foreach (string UserDataKeyName in HklmRootKey.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Installer\UserData").GetSubKeyNames())
{
//Ignore the LocalSystem account
if (UserDataKeyName != "S-1-5-18")
{
RegistryKey ProductsKey = HklmRootKey.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Installer\UserData\" + UserDataKeyName + @"\Products");
if (ProductsKey != null)
{
string[] LmProductGuids = ProductsKey.GetSubKeyNames();
foreach (string LmProductGuid in LmProductGuids)
{
if (LmProductGuid == CuProductGuid)
{
RegistryKey UserDataProgramKey = HklmRootKey.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Installer\UserData\" + UserDataKeyName + @"\Products\" + LmProductGuid + @"\InstallProperties");
if ((int)UserDataProgramKey.GetValue("SystemComponent", 0) != 1)
{
string Name = CuInstallerKey.OpenSubKey(CuProductGuid).GetValue("ProductName", String.Empty).ToString();
string ProgVersion = string.Empty;
try
{
ProgVersion = UserDataProgramKey.GetValue("DisplayVersion", String.Empty).ToString();
}
catch (Exception)
{
throw;
}
if (Name != string.Empty && !IsProgramInList(Name, ExistingProgramList))
{
ExistingProgramList.Add(new InstalledProgram(Name));
ProductFound = true;
}
}
}
}
if (ProductFound)
{
break;
}
try
{
ProductsKey.Close();
}
catch (Exception)
{
throw;
}
}
}
}
}
}
return ExistingProgramList;
}
}
直接使用即可,就是获取到了软件打开路径还有些问题需要优化。
|