CSharp08.Program.BuildUinstallItem C# (CSharp) Метод

BuildUinstallItem() статический приватный Метод

static private BuildUinstallItem ( RegistryKey itemKey, UinstallItem &item ) : bool
itemKey RegistryKey
item UinstallItem
Результат bool
        static bool BuildUinstallItem(RegistryKey itemKey, ref UinstallItem item)
        {
            string IconPEPath = string.Empty;

            // 先准备最容易得到的数据
            item.RegistryKey = itemKey.Name.Substring(itemKey.Name.LastIndexOf('\\') + 1);

            item.DisplayName = GetStringValueFromRegistry(itemKey, "DisplayName");
            // item.QuietUninstall = itemKey.GetValue("QuietUninstallString", string.Empty).ToString();
            item.UninstallCmd = GetStringValueFromRegistry(itemKey, "UninstallString");
            IconPEPath = GetStringValueFromRegistry(itemKey, "DisplayIcon");
            item.InstallFolder = GetStringValueFromRegistry(itemKey, "InstallLocation");
            item.Version = GetStringValueFromRegistry(itemKey, "DisplayVersion");
            item.CompanyName = GetStringValueFromRegistry(itemKey, "Publisher");

            try
            {
                string s = GetStringValueFromRegistry(itemKey, "InstallDate");
                if (!string.IsNullOrEmpty(s))
                {
                    item.InstallDate = DateTime.ParseExact(s, "yyyyMMdd", null);
                }
            }
            catch { item.InstallDate = null; }

            // 得到重要的中间数据PEPath
            string PEPath = string.Empty;
            if (!string.IsNullOrEmpty(IconPEPath))
            {
                Match m = Regex.Match(IconPEPath, "\\p{L}:[^?<>|:\"]+?\\.exe");
                if (m.Success) PEPath = m.Groups[0].ToString();
            }
            if (string.IsNullOrEmpty(PEPath) &&
                !string.IsNullOrEmpty(item.UninstallCmd))
            {
                Match m = Regex.Match(item.UninstallCmd, "\\p{L}:[^?<>|:\"]+?\\.exe");
                if (m.Success) PEPath = m.Groups[0].ToString();
            }
            if (string.IsNullOrEmpty(PEPath) &&
                !string.IsNullOrEmpty(item.InstallFolder))
            {
                string[] files = Directory.GetFiles(item.InstallFolder, "*.exe");
                if (files != null && files.Length > 0)
                {
                    PEPath = files[0];
                }
            }

            // 根据PEPath反过来可以尝试修正一些数据
            if (!string.IsNullOrEmpty(PEPath))
            {
                if (string.IsNullOrEmpty(IconPEPath))
                {
                    IconPEPath = PEPath;
                }
                if (string.IsNullOrEmpty(item.InstallFolder))
                {
                    item.InstallFolder = Path.GetDirectoryName(PEPath);
                }
            }

            // 读取PEPath文件中的内容,修正一些数据
            if (!string.IsNullOrEmpty(PEPath))
            {
                Win32Wrap.FileVersionInfo info = Win32Wrap.GetFileVersionInfo(PEPath);
                if (info.langBasedInfoList != null && info.langBasedInfoList.Length > 0)
                {
                    Win32Wrap.FileVersionInfo.LanguageBasedFileInfo lanBasedInfo = info.langBasedInfoList[0];

                    item.Description = lanBasedInfo.fileDescription;
                    if (string.IsNullOrEmpty(item.DisplayName) &&
                        !string.IsNullOrEmpty(lanBasedInfo.productName))
                    {
                        item.DisplayName = lanBasedInfo.productName;
                    }
                    if (string.IsNullOrEmpty(item.CompanyName) &&
                        !string.IsNullOrEmpty(lanBasedInfo.companyName))
                    {
                        item.CompanyName = lanBasedInfo.companyName;
                    }
                    if (string.IsNullOrEmpty(item.Version) &&
                        !string.IsNullOrEmpty(lanBasedInfo.productVersion))
                    {
                        item.Version = lanBasedInfo.productVersion;
                    }
                }
            }

            if (!string.IsNullOrEmpty(IconPEPath))
            {
                int iconIdx = 0;
                IntPtr[] smallIcon = new IntPtr[1];

                {
                    Match m = Regex.Match(IconPEPath, "(.+),(\\d)$");
                    if (m.Success)
                    {
                        try
                        {
                            IconPEPath = m.Groups[1].ToString();
                            iconIdx = int.Parse(m.Groups[2].ToString());
                        }
                        catch { }
                    }
                }

                if (IconPEPath.Length > 2 &&
                    IconPEPath.StartsWith("\"") &&
                    IconPEPath.EndsWith("\""))
                {
                    IconPEPath = IconPEPath.Substring(1, IconPEPath.Length - 2);
                }

                if (Win32Import.ExtractIconEx(IconPEPath, 0, null, smallIcon, 1) > 0 &&
                    smallIcon[0] != IntPtr.Zero)
                {
                    item.PEIcon = Icon.FromHandle(smallIcon[0]);
                }
            }
            if (item.PEIcon == null)
            {
                IntPtr[] smallIcon = new IntPtr[1];
                if (Win32Import.ExtractIconEx("shell32.dll", 2, null, smallIcon, 1) > 0 &&
                smallIcon[0] != IntPtr.Zero)
                {
                    item.PEIcon = Icon.FromHandle(smallIcon[0]);
                }
            }

            // 修正安装日期
            if (item.InstallDate == null)
            {
                item.InstallDate = Win32Wrap.GetRegstryKeyLastWriteTime(
                    Win32Import.HKEY_LOCAL_MACHINE,
                    itemKey.Name.Substring(itemKey.Name.IndexOf('\\') + 1));
            }

            // 保证显示的数据存在
            if (string.IsNullOrEmpty(item.DisplayName) &&
                !string.IsNullOrEmpty(PEPath))
            {
                item.DisplayName = Path.GetFileNameWithoutExtension(PEPath);
            }

            return !string.IsNullOrEmpty(item.DisplayName);
        }