dotnet 通过 WMI 获取系统安装软件

2022-08-04 19:34:47 浏览数 (1)

本文告诉大家如何通过 WMI 获取系统安装的软件,这个方法不能获取全部的软件

通过 Win32_Product 可以获取系统安装的软件

代码语言:javascript复制
            var mc = "Win32_Product";
            var managementObject = new[]
            {
                    "Caption",
                    "Description",
                    "IdentifyingNumber",
                    "InstallDate",
                    "InstallLocation",
                    "HelpLink",
                    "HelpTelephone",
                    "InstallSource",
                    "Language",
                    "LocalPackage",
                    "Name",
                    "PackageCache",
                    "PackageCode",
                    "PackageName",
                    "ProductID",
                    "RegOwner",
                    "RegCompany",
                    "SKUNumber",
                    "Transforms",
                    "URLInfoAbout",
                    "URLUpdateInfo",
                    "Vendor",
                    "WordCount",
                    "Version",
            };
            ManagementClass managementClass = new ManagementClass(mc);
            ManagementObjectCollection managementObjectCollection = managementClass.GetInstances();
            var str = new StringBuilder();

            foreach (ManagementObject m in managementObjectCollection)
            {
                foreach (var temp in managementObject)
                {
                    try
                    {
                        str.Append(temp);
                        str.Append(" ");
                        str.Append(m[temp]?.ToString() ?? "");
                        str.Append("n");
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(temp   " "   e);
                    }
                }

                str.Append("n");
            }

            return str.ToString();

输出 str 的内容

代码语言:javascript复制
Caption           : 坚果云
Description       : 坚果云
IdentifyingNumber : {FEA8B01C-3F43-470A-BB28-679B1AEEC6E8}
InstallDate       : 20180305
InstallLocation   : C:Program FilesNutstore
HelpLink          : http://help.jianguoyun.com
HelpTelephone     :
InstallSource     : C:UserslindeAppDataRoamingNutstoreClientinstallAEEC6E8
Language          : 2052
LocalPackage      : C:WINDOWSInstaller4acb3a9.msi
Name              : 坚果云
PackageCache      : C:WINDOWSInstaller4acb3a9.msi
PackageCode       : {3802EFD2-0953-4527-835E-E4C459062CD5}
PackageName       : Nutstore.x64.msi
ProductID         :
RegOwner          :
RegCompany        :
SKUNumber         :
Transforms        : C:WINDOWSInstaller{FEA8B01C-3F43-470A-BB28-679B1AEEC6E8}Nutstore.mst
URLInfoAbout      : https://www.jianguoyun.com/
URLUpdateInfo     :
Vendor            : 上海亦存网络科技有限公司
WordCount         : 0
Version           : 4.0.8


Caption           : Apple 应用程序支持 (32 位)
Description       : Apple 应用程序支持 (32 位)
IdentifyingNumber : {5A659BE5-849B-484E-A83B-DCB78407F3A4}
InstallDate       : 20190221
InstallLocation   : C:Program Files (x86)Common FilesAppleApple Application Support
HelpLink          : http://www.apple.com/cn/support/
HelpTelephone     : (86) 800 810 2323
InstallSource     : C:UserslindeAppDataLocalTempIXP246.TMP
Language          : 2052
LocalPackage      : C:WINDOWSInstaller1a1ef7b.msi
Name              : Apple 应用程序支持 (32 位)
PackageCache      : C:WINDOWSInstaller1a1ef7b.msi
PackageCode       : {F3D0B996-B6DB-4283-9565-004518A6610B}
PackageName       : AppleApplicationSupport.msi
ProductID         :
RegOwner          :
RegCompany        :
SKUNumber         :
Transforms        :
URLInfoAbout      : http://www.apple.com/cn/
URLUpdateInfo     : http://www.apple.com/cn/
Vendor            : Apple Inc.
WordCount         : 0
Version           : 7.3

0 人点赞