• 收集电脑上安装的软件列表


    公司的IT工程师某一天找我聊天,说有没有一个程序能够统计电脑上安装的软件,公司采用的是域控,然后把这个软件放在域控的组策略里面,设置一番,只要登录域控的时候自动运行一遍,然后把采集的信息写入共享目录,这样就不用一台一台的统计了。
    当时一想就直接用C#写了一个控制台程序。代码如下:

    static void Main(string[] args)
            {
                string path = @"\192.168.10.251\hard_info\";
                StringBuilder proinfo = new StringBuilder();
                string uninstallKey = @"SOFTWAREMicrosoftWindowsCurrentVersionUninstall";
                using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(uninstallKey))
                {
                    foreach (string skName in rk.GetSubKeyNames())
                    {
                        using (RegistryKey sk = rk.OpenSubKey(skName))
                        {
                            try
                            {
                                var displayName = sk.GetValue("DisplayName");
                                if (displayName != null)
                                {
                                    proinfo.AppendLine(displayName.ToString());
                                }
                            }
                            catch 
                            {
                            }
                        }
                    }
                }
                File.WriteAllText(path + Dns.GetHostName() + ".txt", proinfo.ToString());
                Environment.Exit(0);
            }
    View Code

    运行以后,是能统计出来,但是少了很多软件,只有一部分,网上查询一番,发现要区分64位操作系统和32位系统,还要区分是LocalMachine还是CurrentUser

    针对x86系统

    HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrentVersionUninstall

    HKEY_CURRENT_USERSOFTWAREMicrosoftWindowsCurrentVersionUninstall


    针对x64系统

    HKEY_LOCAL_MACHINESOFTWAREWow6432NodeMicrosoftWindowsCurrentVersionUninstall

    HKEY_CURRENT_USERSOFTWAREWow6432NodeMicrosoftWindowsCurrentVersionUninstall

    根据以上,改进以后代码如下:

    static void Main(string[] args)
            {
                string path = @"\192.168.10.251\hard_info\";
                StringBuilder proinfo = new StringBuilder();
                proinfo = ShowAllSoftwaresName();
              
                File.WriteAllText(path + Dns.GetHostName() + ".txt", proinfo.ToString());
                Environment.Exit(0);
            }
            public static StringBuilder DisplayInstalledApps(RegistryKey key)
            {
                StringBuilder result = new StringBuilder();
                string displayName;
                if (key != null)
                {
                    foreach (String keyName in key.GetSubKeyNames())
                    {
                        RegistryKey subkey = key.OpenSubKey(keyName);
                        displayName = subkey.GetValue("DisplayName") as string;
                        if (!string.IsNullOrEmpty(displayName))
                        {
                            result.AppendLine(displayName);
                        }
                    }
                }
                return result;
            }
    
            public static StringBuilder ShowAllSoftwaresName()
            {
                StringBuilder proinfo = new StringBuilder();
                RegistryKey key;
    
                // search in: CurrentUser
                key = Registry.CurrentUser.OpenSubKey(@"SOFTWAREMicrosoftWindowsCurrentVersionUninstall");
                proinfo.Append(DisplayInstalledApps(key));
                // search in: LocalMachine_32
                key = Registry.LocalMachine.OpenSubKey(@"SOFTWAREMicrosoftWindowsCurrentVersionUninstall");
                proinfo.Append(DisplayInstalledApps(key));
                // search in: CurrentUser_64
                key = Registry.CurrentUser.OpenSubKey(@"SOFTWAREWow6432NodeMicrosoftWindowsCurrentVersionUninstall");
                proinfo.Append(DisplayInstalledApps(key));
                // search in: LocalMachine_64
                key = Registry.LocalMachine.OpenSubKey(@"SOFTWAREWow6432NodeMicrosoftWindowsCurrentVersionUninstall");
                proinfo.Append(DisplayInstalledApps(key));
                return proinfo;
            }
    View Code

    运行以后,全部采集了电脑安装的软件。

    发给IT工程师后,反馈:有部分电脑运行不了。。。xp系统不支持。。。系统默认没安装framework

    81aa447bef55791ac666a43e763eccf3(1)

    最后和IT工程师商议后,可以使用VBS,以前也写过VBS,网上找找资料就写了一个,完美解决了。

    附件为vbs脚本:vbs获取软件列表.zip

    参考资料:

    VBScript:检查目标计算机软件列表

    PowerShell快速高效地获取安装的软件列表

    Uninstall Registry Key

  • 相关阅读:
    内蒙古草原之行
    【iOS开发笔记25/50】:正则表达式
    读书笔记:《写给大家看的设计书》
    【iOS开发笔记22/50】:恼人的a valid provisioning profile for this executable was not found错误
    【搞定GTD】打造高效的OmniFocus系统
    【iOS开发笔记24/50】调整UIImage的大小
    【iOS开发笔记26/50】我自己写的苹果应用程序XQViewer终于上架了,解决了一系列的问题,终于挺过来了
    桥牌笔记:双挤
    养成一个习惯需要几年,而毁掉一下习惯只需要一天
    使用SuperMemo背单词2000天,抓图纪念一下!
  • 原文地址:https://www.cnblogs.com/1024zy/p/6019852.html
Copyright © 2020-2023  润新知