C#获取32位或者64位系统安装软件信息

来源:互联网 发布:天府广场美食 知乎 编辑:程序博客网 时间:2024/05/24 05:25
using System;using System.Collections.Generic;using System.Text;using Jxmstc.common.Network;using System.Collections;using Microsoft.Win32;using System.Windows.Forms;using System.IO;using System.Web;using System.Data;using Jxmstc.scvmm.Utils;using System.Management;namespace Jxmstc.vm.operation{    /*     *获取虚拟机中安装软件信息     */    class InstallSoftData : BaseOpertaion    {        private string sReturnMessage = string.Empty;        HttpProcessor p;        string addressWidth = String.Empty;        private new static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);        public override void Excute(Hashtable htParameters, HttpProcessor _p)        {            //保存返回的Json格式安装的软件信息            p = _p;            string bit32 = string.Empty;            string bit64 = string.Empty;            RegistryKey localMachine;            RegistryKey Uninstall;            string sInstallSoftsData = "";            //将客户机中安装的软件信息保存在DataTable中            DataTable dt = new DataTable();            dt.Columns.Add("SoftName");            dt.Columns.Add("SoftVersion");            dt.Columns.Add("SoftPublisher");                        string sIs64or32System = Distinguish64or32System();                        if (sIs64or32System.Equals("32"))            {                // 如果是32位操作系统,(或者系统是64位,程序也是64位)                bit32 = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";                localMachine = Registry.LocalMachine;                Uninstall = localMachine.OpenSubKey(bit32, true);            }            else if (sReturnMessage.Equals("default"))            {                //默认为32位                bit32 = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";                localMachine = Registry.LocalMachine;                Uninstall = localMachine.OpenSubKey(bit32, true);            }            else            {                // 如果操作系统是64位并且程序是64位的                bit64 = @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall";                localMachine = Registry.LocalMachine;                Uninstall = localMachine.OpenSubKey(bit64, true);            }            DataTable _dt = ListInstallSofts(Uninstall, dt, sIs64or32System);            sInstallSoftsData = ToJson.DataTableToJSON(_dt);            p.WriteSuccess();            p.outputStream.WriteLine(HttpUtility.UrlEncode("Success"));            p.outputStream.WriteLine(sInstallSoftsData);        }        private  DataTable ListInstallSofts(RegistryKey Uninstall, DataTable dt, String is32Or64)        {            foreach (string subkey in Uninstall.GetSubKeyNames())            {                try                {                    if (subkey == null)                    {                        continue;                    }                    else                    {                        RegistryKey currentKey = Uninstall.OpenSubKey(subkey);                        DataRow dr = dt.NewRow();                        dr["SoftName"] = currentKey.GetValue("DisplayName");                        dr["SoftVersion"] = currentKey.GetValue("displayversion");                        dr["SoftPublisher"] = currentKey.GetValue("publisher");                        if (dr["SoftName"].ToString().Equals("") || dr["SoftName"].ToString().Contains("Update") || dr["SoftName"].ToString().Contains("SP1") || dr["SoftName"].ToString().Contains("SP3") || dr["SoftName"].ToString().Contains("修补程序") || dr["SoftName"].ToString().Contains("安全更新") || dr["SoftName"].ToString().Contains("更新"))                        { }                        else if (DeleteTheValue(dt, dr["SoftName"].ToString()))                        { }                        else                            dt.Rows.Add(dr);                    }                }                catch (Exception ex)                {                    p.WriteSuccess();                    p.outputStream.WriteLine(ex.Message);                    log.Debug(ex.Message);                    log.Info(ex.Message);                }            }            Uninstall.Close();            //64位操作系统安装了32位软件            if(is32Or64.Equals("64"))            {                const string bit32 = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";                RegistryKey localMachine = Registry.LocalMachine;                Uninstall = localMachine.OpenSubKey(bit32, true);                ListInstallSofts(Uninstall, dt, "32");                Uninstall.Close();            }            return dt;        }        private static bool DeleteTheValue(DataTable dt, string softName)        {            for (int i = 0; i < dt.Rows.Count; i++)            {                if (dt.Rows[i][0].ToString().Equals(softName))                {                    return true;                }            }            return false;        }        private string Distinguish64or32System()        {            try            {                ConnectionOptions mConnOption = new ConnectionOptions();                ManagementScope mMs = new ManagementScope("\\\\localhost", mConnOption);                ObjectQuery mQuery = new ObjectQuery("select AddressWidth from Win32_Processor");                ManagementObjectSearcher mSearcher = new ManagementObjectSearcher(mMs, mQuery);                ManagementObjectCollection mObjectCollection = mSearcher.Get();                foreach (ManagementObject mObject in mObjectCollection)                {                    addressWidth = mObject["AddressWidth"].ToString();                }                return addressWidth;            }            catch (Exception ex)            {                p.WriteSuccess();                p.outputStream.WriteLine(ex.Message);                log.Debug(ex.Message);                log.Info(ex.Message);                sReturnMessage = "default";                return sReturnMessage;            }        }    }}

原创粉丝点击