首页 > 技术文章 > 收集电脑上安装的软件列表

1024zy 2016-11-01 16:16 原文

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

static void Main(string[] args)
        {
            string path = @"\\192.168.10.251\\hard_info\\";
            StringBuilder proinfo = new StringBuilder();
            string uninstallKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
            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_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall

HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall


针对x64系统

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall

HKEY_CURRENT_USER\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall

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

 

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(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall");
            proinfo.Append(DisplayInstalledApps(key));
            // search in: LocalMachine_32
            key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall");
            proinfo.Append(DisplayInstalledApps(key));
            // search in: CurrentUser_64
            key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall");
            proinfo.Append(DisplayInstalledApps(key));
            // search in: LocalMachine_64
            key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall");
            proinfo.Append(DisplayInstalledApps(key));
            return proinfo;
        }
View Code

 

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

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

81aa447bef55791ac666a43e763eccf3(1)

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

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

参考资料:

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

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

Uninstall Registry Key

推荐阅读