C#程序开发,检测当前电脑已经安装的软件目录

在Windows中使用C#做软件开发,有时候需要获取当前系统中已安装的软件及其版本号,接下来就简单介绍一下,如何通过C#获取来实现。

1、通过注册表

csharp 复制代码
public List<PcSoftinfo> GetAllInstalledSoftware(string name)
{
    var keys = new RegistryKey[]
    {
                 Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Installer\Products"),
              Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Installer\Products"),
              Registry.ClassesRoot.OpenSubKey(@"Installer\Products"),
             
                Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"),
                Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"),
                Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"),
                Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall")
    };
    var softs = new List<PcSoftinfo>();
    foreach (var k in keys)
    {
        if (k == null)
            continue;
        foreach (var keyName in k.GetSubKeyNames())
        {
            RegistryKey subkey = k.OpenSubKey(keyName);
            var displayName = subkey.GetValue("DisplayName") as string;
            //var productName = subkey.GetValue("ProductName") as string;不同注册表名称不一样。
            if (!string.IsNullOrEmpty(displayName))
            {
                int systemComponent = (int)subkey.GetValue("SystemComponent", 0);
                if (systemComponent == 1)
                {
                    //系统应用排除
                    //continue;
                }

                var displayVersion = subkey.GetValue("DisplayVersion") as string;
                if (!string.IsNullOrEmpty(displayVersion))
                {
                    softs.Add(new PcSoftinfo() { Name = displayName, Ver = displayVersion });
                }
            }
        }
    }


    return softs;
}

优点快,缺点不完全,有些软件读不到。

2、通过Win32_Product

csharp 复制代码
        public List<PcSoftinfo> IsSoftwareInstalled(string softwareName)
        {
            // 创建WMI查询,检查Win32_Product类中的安装信息
            //string query = "SELECT Name, Version FROM Win32_Product WHERE Vendor ='厂家名称'";
            string query = "SELECT Name, Version FROM Win32_Product";
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
            var softs = new List<PcSoftinfo>();
            foreach (ManagementObject obj in searcher.Get())
            {
                // 如果找到匹配的软件
                string NAME = obj["Name"].ToString();
                string version = obj["Version"]?.ToString();
                softs.Add(new PcSoftinfo() { Name = NAME, Ver = version });
            }

            return softs; // 没找到软件
        }

优点全,缺点慢。

相关推荐
Ray Liang8 小时前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
Scout-leaf3 天前
WPF新手村教程(三)—— 路由事件
c#·wpf
用户298698530143 天前
程序员效率工具:Spire.Doc如何助你一键搞定Word表格排版
后端·c#·.net
mudtools5 天前
搭建一套.net下能落地的飞书考勤系统
后端·c#·.net
玩泥巴的5 天前
搭建一套.net下能落地的飞书考勤系统
c#·.net·二次开发·飞书
唐宋元明清21885 天前
.NET 本地Db数据库-技术方案选型
windows·c#
郑州光合科技余经理5 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
lindexi5 天前
dotnet DirectX 通过可等待交换链降低输入渲染延迟
c#·directx·d2d·direct2d·vortice
feifeigo1235 天前
matlab画图工具
开发语言·matlab
dustcell.5 天前
haproxy七层代理
java·开发语言·前端