C#获取已安装程序列表
查找注册表:SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall下的所有项
需要查找的位置为:HKEY_LOCAL_MACHINE和HKEY_CURRENT_USER并且要查找32位和64位的视图,而且要注意排除系统程序,可以查找SystemComponent值为1的排除掉。示例代码如下:
/// <summary>
/// 注册表操作
/// </summary>
public static class RegHelper
{
#region 属性
public const string UninstallKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
#endregion
#region 私有变量
#endregion
#region 构造函数
#endregion
#region 共有方法
public static List<SoftModel> FindSoft()
{
List<SoftModel> lst = new List<SoftModel>();
RegistryKey regUninstall = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(UninstallKey, false);
FindSoft(regUninstall, ref lst, RegistryView.Registry32);
regUninstall = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64).OpenSubKey(UninstallKey, false);
FindSoft(regUninstall, ref lst, RegistryView.Registry64);
regUninstall = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry32).OpenSubKey(UninstallKey, false);
FindSoft(regUninstall, ref lst, RegistryView.Registry32);
regUninstall = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64).OpenSubKey(UninstallKey, false);
FindSoft(regUninstall, ref lst, RegistryView.Registry64);
return lst;
}
#endregion
#region 私有方法
private static void FindSoft(RegistryKey regUninstall, ref List<SoftModel> lst, RegistryView registryView)
{
foreach (var item in regUninstall.GetSubKeyNames())
{
RegistryKey regSub = regUninstall.OpenSubKey(item, false);
string displayName = regSub.GetValue("DisplayName") as string;
string installLocation = regSub.GetValue("InstallLocation") as string;
string uninstallString = regSub.GetValue("UninstallString") as string;
string displayVersion = regSub.GetValue("DisplayVersion") as string;
string installDate = regSub.GetValue("InstallDate") as string;
string publisher = regSub.GetValue("Publisher") as string;
string displayIcon = regSub.GetValue("DisplayIcon") as string;
int estimatedSize = (int)regSub.GetValue("EstimatedSize", 0);
int systemComponent = (int)regSub.GetValue("SystemComponent", 0);
do
{
if (string.IsNullOrWhiteSpace(displayName))
{
break;
}
if (string.IsNullOrWhiteSpace(uninstallString))
{
break;
}
if (string.IsNullOrWhiteSpace(displayVersion) && string.IsNullOrWhiteSpace(displayIcon))
{
break;
}
if(systemComponent == 1) // 排除系统应用
{
break;
}
if (string.IsNullOrWhiteSpace(installDate) && !string.IsNullOrWhiteSpace(displayIcon))
{
try
{
string[] array = displayIcon.Split(',');
if(array.Length >= 2)
{
uninstallString = array[0];
}
FileInfo fileInfo = new FileInfo(uninstallString);
installDate = fileInfo.CreationTime.ToShortDateString(); //使用文件创建时间作为安装时间
}
catch(Exception ex)
{
}
}
SoftModel softModel = lst.FirstOrDefault(item1 => item1.Name == displayName);
if(softModel == null)
{
SoftModel model = new SoftModel
{
Name = displayName,
Version = displayVersion,
DateTime = installDate,
UninstallString = uninstallString,
Publisher = publisher,
EstimatedSize = estimatedSize == 0 ? "未知" : (estimatedSize / 1024.0).ToString("0.00M"),
RegKey = regSub.ToString(),
InstallLocation = installLocation,
RegistryView = registryView,
};
lst.Add(model);
}
else
{
if(string.IsNullOrWhiteSpace(softModel.DateTime) && !string.IsNullOrWhiteSpace(installDate))
{
softModel.DateTime = installDate;
}
}
} while (false);
}
}
#endregion
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
2020-01-13 计划任务