IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> Unity 使用注册表获取电脑安装所有软件 -> 正文阅读

[游戏开发]Unity 使用注册表获取电脑安装所有软件

在网上查了很久,看到有很多通过注册表获取已安装软件列表,但是我试了下获取的都不全。

找到了一个使用VB写的程序是可以获取全部安装软件列表的,那个文章我找不到了,回头找到再贴出来。我这边用C#实现了他的代码的功能,也是可以获取所有软件的。

using Microsoft.Win32;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using UnityEngine;

public class InstalledProgram
{
   
    public InstalledProgram(string ProgramDisplayName)
    {
        this.DisplayName = ProgramDisplayName;
    }
    public InstalledProgram(string ProgramDisplayName, string ProgramParentDisplayName , bool IsProgramUpdate, string ProgramVersion,string ProgramInstallLocation)
    {
        this.DisplayName = ProgramDisplayName;
        this.ParentDisplayName = ProgramParentDisplayName;
        this.IsUpdate = IsProgramUpdate;
        this.Version = ProgramVersion;
        this.InstallLocation = ProgramInstallLocation;
    }

    private string _DisplayName = string.Empty;
    /// <summary>
    /// The name that would be displayed in Add/Remove Programs
    /// </summary>
    public string DisplayName
    {
        get
        {
            return _DisplayName;
        }
        set
        {
            _DisplayName = value;
        }
    }

    private string _Version = string.Empty;
    /// <summary>
    /// The version of the program
    /// </summary>
    public string Version
    {
        get
        {
            return _Version;
        }
        set
        {
            _Version = value;
        }
    }


    private string _ParentDisplayName = string.Empty;
    /// <summary>
    /// If this program is an update then this will contain the display name of the product that this is an update to
    /// </summary>
    public string ParentDisplayName
    {
        get
        {
            return _ParentDisplayName;
        }
        set
        {
            _ParentDisplayName = value;
        }
    }

    private bool _IsUpdate;
    /// <summary>
    ///  Is this program classed as an update 
    /// </summary>
    public bool IsUpdate
    {
        get
        {
            return _IsUpdate;
        }
        set
        {
            _IsUpdate = value;
        }
    }

    private string _InstallLocation = string.Empty;
    /// <summary>
    /// 安装位置
    /// </summary>
    public string InstallLocation {

        get
        {
            return _InstallLocation;
        }
        set
        {
            _InstallLocation = value;
        }
    }
}


public class GetWindowSoft : MonoBehaviour
{

    private void Start()
    {
        GetAllInstalledProgram();
    }


    /// <summary>
    /// 获取电脑安装的所有程序
    /// </summary>
    public void GetAllInstalledProgram()
    {
        List<InstalledProgram> ProgramList = new List<InstalledProgram>();
        RegistryKey ClassesKey = Registry.LocalMachine.OpenSubKey(@"Software\Classes\Installer\Products");
        //---Wow64 Uninstall key
        RegistryKey Wow64UninstallKey = Registry.LocalMachine.OpenSubKey(@"Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall");
        ProgramList = GetUninstallKeyPrograms(Wow64UninstallKey, ClassesKey, ProgramList, true);
        //---Standard Uninstall key
        RegistryKey StdUninstallKey = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall");
        ProgramList = GetUninstallKeyPrograms(StdUninstallKey, ClassesKey, ProgramList, true);

        foreach (string UserSid in Registry.Users.GetSubKeyNames())
        {
            //---HKU Uninstall key
            RegistryKey CuUnInstallKey = Registry.Users.OpenSubKey(UserSid + @"\Software\Microsoft\Windows\CurrentVersion\Uninstall");
            ProgramList = GetUninstallKeyPrograms(CuUnInstallKey, ClassesKey, ProgramList, true);
            //---HKU Installer key
            RegistryKey CuInstallerKey = Registry.Users.OpenSubKey(UserSid + @"\Software\Microsoft\Installer\Products");
            ProgramList = GetUserInstallerKeyPrograms(CuInstallerKey, Registry.LocalMachine, ProgramList);
        }
        foreach (var item in ProgramList)
        {
            print(item.DisplayName+"  --   "+item.InstallLocation);
        }
        try
        {
            Registry.Users.Close();
            Registry.LocalMachine.Close();
        }
        catch (Exception)
        {
            throw;
        }

    }

    List<InstalledProgram> GetUninstallKeyPrograms(RegistryKey UninstallKey, RegistryKey ClassesKey, List<InstalledProgram> ExistingProgramList, bool IncludeUpdates)
    {
        if (UninstallKey != null)
        {
            //'Loop through all subkeys (each one represents an installed program)
            foreach (string SubKeyName in UninstallKey.GetSubKeyNames())
            {

                try
                {
                    RegistryKey CurrentSubKey = UninstallKey.OpenSubKey(SubKeyName);
                    //'Skip this program if the SystemComponent flag is set
                    int IsSystemComponent = 0;
                    try
                    {
                        IsSystemComponent = (int)(CurrentSubKey.GetValue("SystemComponent", 0));
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                    if (IsSystemComponent != 1)
                    {
                        //If the WindowsInstaller flag is set then add the key name to our list of Windows Installer GUIDs
                        if ((int)(CurrentSubKey.GetValue("WindowsInstaller", 0)) != 1)
                        {
                            // Regex WindowsUpdateRegEx = new Regex();
                            string ProgramReleaseType = (CurrentSubKey.GetValue("ReleaseType", String.Empty)).ToString();
                            //print("ProgramReleaseType:"+ProgramReleaseType);
                            string ProgVersion = string.Empty;
                            try
                            {
                                ProgVersion = (CurrentSubKey.GetValue("DisplayVersion", String.Empty)).ToString();
                            }
                            catch (Exception)
                            {
                                throw;
                            }
                            //Check to see if this program is classed as an update
                            if (Regex.Match(SubKeyName, "KB[0-9]{6}$").Success ||
                                (CurrentSubKey.GetValue("ParentKeyName", String.Empty)).ToString() != string.Empty ||
                                (ProgramReleaseType == "Security Update") ||
                                (ProgramReleaseType == "Update Rollup") ||
                                (ProgramReleaseType == "Hotfix"))
                            {
                                if (IncludeUpdates)
                                {
                                    string Name = CurrentSubKey.GetValue("DisplayName", String.Empty).ToString();

                                    if (Name != string.Empty && !IsProgramInList(Name, ExistingProgramList))
                                    {
                                        
                                        ExistingProgramList.Add(new InstalledProgram(Name, CurrentSubKey.GetValue("ParentDisplayName", String.Empty).ToString(), true, ProgVersion, GetInstallLocation(CurrentSubKey)));
                                    }
                                }
                            }
                            else
                            {
                                bool UninstallStringExists = false;
                                foreach (string valuename in CurrentSubKey.GetValueNames())
                                {
                                    if (string.Equals("UninstallString", valuename, StringComparison.CurrentCultureIgnoreCase))
                                    {
                                        UninstallStringExists = true;
                                    }
                                }
                                if (UninstallStringExists)
                                {
                                    string Name = CurrentSubKey.GetValue("DisplayName", String.Empty).ToString();
                                    if (Name != string.Empty && !IsProgramInList(Name, ExistingProgramList))
                                    {
                                        ExistingProgramList.Add(new InstalledProgram(Name, CurrentSubKey.GetValue("ParentDisplayName", String.Empty).ToString(), false, ProgVersion, GetInstallLocation(CurrentSubKey)));
                                    }
                                }
                            }
                        }
                        else//If WindowsInstaller
                        {
                            string ProgVersion = string.Empty;
                            string Name = string.Empty;
                            bool FoundName = false;
                            try
                            {
                                string MsiKeyName = GetInstallerKeyNameFromGuid(SubKeyName);
                                RegistryKey CrGuidKey = ClassesKey.OpenSubKey(MsiKeyName);
                                if (CrGuidKey != null)
                                {
                                    Name = CrGuidKey.GetValue("ProductName", String.Empty).ToString();
                                }
                            }
                            catch (Exception) { }
                            try
                            {
                                ProgVersion = (CurrentSubKey.GetValue("DisplayVersion", String.Empty)).ToString();
                            }
                            catch (Exception)
                            {

                                throw;
                            }
                            if (Name != string.Empty && !IsProgramInList(Name, ExistingProgramList))
                            {
                                ExistingProgramList.Add(new InstalledProgram(Name, CurrentSubKey.GetValue("ParentDisplayName", String.Empty).ToString(), true, ProgVersion, GetInstallLocation(CurrentSubKey)));
                            }
                        }
                    }
                }
                catch (Exception) {

                    throw;
                }
            }
        }
        return ExistingProgramList;
    }

    /// <summary>
    /// 获取软件打开路径(不是完全正确的)
    /// </summary>
    /// <param name="registryKey"></param>
    /// <returns></returns>
    string GetInstallLocation( RegistryKey registryKey)
    {
        string location = string.Empty;
        string DisplayIcon = registryKey.GetValue("DisplayIcon", string.Empty).ToString();
        string DisplayName = registryKey.GetValue("DisplayName", string.Empty).ToString();
        string InstallLocation = registryKey.GetValue("InstallLocation", string.Empty).ToString();
        string InstallSource = registryKey.GetValue("InstallSource", string.Empty).ToString();

        if (DisplayIcon.Contains(".exe"))
        {
            string[] loc = DisplayIcon.Split(',');//去掉后面的“,0”
            location= loc[0];
        }
        else if (InstallLocation != string.Empty)
        {
            location = InstallLocation + @"\" + DisplayName + ".exe";
        }
        if (InstallSource != string.Empty)
        {
            location = InstallSource+ DisplayName + ".exe";
        }
        return location;
    }


    bool IsProgramInList(string ProgramName, List<InstalledProgram> ListToCheck)
    {
        return ListToCheck.Contains(new InstalledProgram(ProgramName));
    }
    string GetInstallerKeyNameFromGuid(string GuidName)
    {
        string[] MsiNameParts = GuidName.Replace("{", "").Replace("}", "").Split('-');
        StringBuilder MsiName = new StringBuilder();
        //'Just reverse the first 3 parts
        for (int i = 0; i <= 2; i++)
        {
            MsiName.Append(ReverseString(MsiNameParts[i]));
        }
        //For the last 2 parts, reverse each character pair
        for (int j = 3; j <= 4; j++)
        {
            for (int i = 0; i < MsiNameParts[j].Length; i++)
            {
                MsiName.Append(MsiNameParts[j][i + 1]);
                MsiName.Append(MsiNameParts[j][i]);
                i += 1;
            }

        }
        return MsiName.ToString();
    }

    string ReverseString(string input)
    {
        char[] chars = input.ToCharArray();
        Array.Reverse(chars);
        return chars.ToString();
    }

    List<InstalledProgram> GetUserInstallerKeyPrograms(RegistryKey CuInstallerKey, RegistryKey HklmRootKey, List<InstalledProgram> ExistingProgramList)
    {
        if (CuInstallerKey != null)
        {
            foreach (string CuProductGuid in CuInstallerKey.GetSubKeyNames())
            {
                bool ProductFound = false;
                foreach (string UserDataKeyName in HklmRootKey.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Installer\UserData").GetSubKeyNames())
                {
                    //Ignore the LocalSystem account
                    if (UserDataKeyName != "S-1-5-18")
                    {
                        RegistryKey ProductsKey = HklmRootKey.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Installer\UserData\" + UserDataKeyName + @"\Products");
                        if (ProductsKey != null)
                        {
                            string[] LmProductGuids = ProductsKey.GetSubKeyNames();
                            foreach (string LmProductGuid in LmProductGuids)
                            {
                                if (LmProductGuid == CuProductGuid)
                                {
                                    RegistryKey UserDataProgramKey = HklmRootKey.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Installer\UserData\" + UserDataKeyName + @"\Products\" + LmProductGuid + @"\InstallProperties");
                                    if ((int)UserDataProgramKey.GetValue("SystemComponent", 0) != 1)
                                    {
                                        string Name = CuInstallerKey.OpenSubKey(CuProductGuid).GetValue("ProductName", String.Empty).ToString();
                                        string ProgVersion = string.Empty;
                                        try
                                        {
                                            ProgVersion = UserDataProgramKey.GetValue("DisplayVersion", String.Empty).ToString();
                                        }
                                        catch (Exception)
                                        {

                                            throw;
                                        }
                                        if (Name != string.Empty && !IsProgramInList(Name, ExistingProgramList))
                                        {
                                            ExistingProgramList.Add(new InstalledProgram(Name));
                                            ProductFound = true;
                                        }
                                    }
                                }
                            }
                            if (ProductFound)
                            {
                                break;
                            }
                            try
                            {
                                ProductsKey.Close();
                            }
                            catch (Exception)
                            {

                                throw;
                            }
                        }
                    }
                }
            }
        }

        return ExistingProgramList;
    }

}

直接使用即可,就是获取到了软件打开路径还有些问题需要优化。

  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2022-01-08 14:22:27  更:2022-01-08 14:23:46 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/27 19:54:33-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码