InstallInfoHelper.cs 6.02 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Install.Core.Common
{
    public static class InstallInfoHelper
    {
        /// <summary>
        /// 从注册表获取已经安装程序信息
        /// </summary>
        /// <returns></returns>
        public static List<InstallInfo> GetInstallInfos()
        {
            List<InstallInfo> installInfos = new List<InstallInfo>();

            RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\FLYAutomation"); //该项必须已存在
            if (key == null)
            {
                //没有安装任何软件
                return installInfos;
            }
            
            string[] subKeyNames = key.GetSubKeyNames();
            for (int i = 0; i < subKeyNames.Count(); i++)
            {
                string subKeyName = subKeyNames[i];
                var subKey = key.OpenSubKey(subKeyName);
                string exePath = subKey.GetValue("ExePath") as string;
                string installPath = subKey.GetValue("InstallPath") as string;
                string name = subKey.GetValue("Name") as string;

                //版本信息从实际的执行文件获取
                //string version = subKey.GetValue("Version") as string;
                if ((exePath == null) || (installPath == null) || (name == null))
                    continue;

                string path = System.IO.Path.Combine(installPath, exePath);
                if (!File.Exists(path))
                {
                    //执行文件不存在,这个没有用的安装信息   
                    continue;
                }

                FileVersionInfo fileVerInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(path);
                //版本号显示为“主版本号.次版本号.内部版本号.专用部件号”。
                string version = String.Format("{0}.{1}.{2}.{3}", fileVerInfo.FileMajorPart, fileVerInfo.FileMinorPart, fileVerInfo.FileBuildPart, fileVerInfo.FilePrivatePart);
                    
                InstallInfo installInfo = new InstallInfo()
                {
                    InstallPath = installPath,
                    Exe = System.IO.Path.GetFileName(exePath),
                    Name = name,
                    Version = version
                };
                installInfo.IsAutoRun = CheckAutoRun(installInfo);
                installInfos.Add(installInfo);
                
            }
            return installInfos;
        }

        /// <summary>
        /// 从路径获取已经安装程序信息
        /// </summary>
        /// <returns></returns>
        public static List<InstallInfo> GetInstallInfosFromPath(InstallPackCollection installPackCollection)
        {
            List<InstallInfo> installInfos = new List<InstallInfo>();

            string installBasePath = installPackCollection.DefaultInstallPath;

            if (!Directory.Exists(installBasePath))
                return installInfos;




            foreach (var installPack in installPackCollection.Items) { 

                string installPath = $@"{installBasePath}\{installPack.ProcessName}";
                string filePath = $@"{installPath}\{installPack.Exe}";
                if (!File.Exists(filePath))
                    continue;//这个软件不存在

                FileVersionInfo fileVerInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(filePath);
                //版本号显示为“主版本号.次版本号.内部版本号.专用部件号”。
                string version = String.Format("{0}.{1}.{2}.{3}", fileVerInfo.FileMajorPart, fileVerInfo.FileMinorPart, fileVerInfo.FileBuildPart, fileVerInfo.FilePrivatePart);

                InstallInfo installInfo = new InstallInfo()
                {
                    InstallPath = installPath,
                    Exe = System.IO.Path.GetFileName(installPack.Exe),
                    Name = installPack.Name,
                    Version = version
                };
                installInfo.IsAutoRun = CheckAutoRun(installInfo);

                //补充安装信息
                SetInstallInfo(installInfo);


                installInfos.Add(installInfo);

            }

            return installInfos;
        }



        public static bool SetInstallInfo(InstallInfo item)
        {
            RegistryKey key3 = Registry.CurrentUser.OpenSubKey($@"Software\FLYAutomation\{item.KeyName}", true);
            if (key3 == null)
                key3 = Registry.CurrentUser.CreateSubKey($@"Software\FLYAutomation\{item.KeyName}", RegistryKeyPermissionCheck.ReadWriteSubTree);

            key3.SetValue("Name", item.Name);
            key3.SetValue("ExePath", item.ExePath);
            key3.SetValue("InstallPath", item.InstallPath);
            key3.SetValue("Version", item.Version);
            key3.Close();
            return true;
        }
        public static void RemoveInstallInfo(InstallInfo item)
        {
            Registry.CurrentUser.DeleteSubKey(@"Software\FLYAutomation\" + item.KeyName, false);
        }
        public static void SetAutoRun(InstallInfo item)
        {
            RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
            key.SetValue(item.KeyName, item.ShortcutPath);
            key.Close();
        }
        public static bool CheckAutoRun(InstallInfo item)
        {
            RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");

            object o = key.GetValue(item.KeyName);
            key.Close();
            if (string.IsNullOrEmpty(o as string))
                return false;
            else
                return true;
        }
        public static void RemoveAutoRun(InstallInfo item)
        {
            RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
            key.DeleteValue(item.KeyName, false);
            key.Close();
        }
    }

}