static List <UinstallItem> PrepareUinstallList() { List <UinstallItem> list = new List <UinstallItem>(); using (RegistryKey uinstallKey = OpenUninstallRegistryKey(false)) { string[] subKeyNames = uinstallKey.GetSubKeyNames(); // 显示等待对话框 Form form = new Form(); form.StartPosition = FormStartPosition.CenterScreen; form.Size = new Size(250, 50); form.MinimizeBox = false; form.MaximizeBox = false; form.Text = "正在查找已经安装的软件..."; ProgressBar bar = new ProgressBar(); bar.Dock = DockStyle.Fill; bar.Maximum = 100; bar.Minimum = 0; bar.Value = bar.Minimum; form.Controls.Add(bar); BackgroundWorker worker = new BackgroundWorker(); worker.WorkerReportsProgress = true; worker.DoWork += (o, e) => { for (int i = 0; i < subKeyNames.Length; ++i) { string subKeyName = subKeyNames[i]; using (RegistryKey subKey = uinstallKey.OpenSubKey(subKeyName, false)) { UinstallItem item = new UinstallItem(); if (BuildUinstallItem(subKey, ref item)) { list.Add(item); } } worker.ReportProgress(i * 100 / subKeyNames.Length); } }; worker.ProgressChanged += (o, e) => { bar.Value = e.ProgressPercentage; }; worker.RunWorkerCompleted += (o, e) => { form.Close(); }; worker.RunWorkerAsync(); form.ShowDialog(); worker.Dispose(); form.Dispose(); } return(list); }
static void TryUninstallItem(UinstallItem item) { Form form = new Form(); form.Size = new Size(350, 300); form.Text = item.DisplayName; form.StartPosition = FormStartPosition.CenterScreen; FlowLayoutPanel btnPanel = new FlowLayoutPanel(); btnPanel.Dock = DockStyle.Bottom; btnPanel.FlowDirection = FlowDirection.LeftToRight; btnPanel.BorderStyle = BorderStyle.FixedSingle; btnPanel.Height = 30; Button btnInstallFolder = new Button() { Text = "安装目录" }; btnInstallFolder.Enabled = !string.IsNullOrEmpty(item.InstallFolder); btnInstallFolder.Click += (o, e) => { Win32Import.ShellExecute(form.Handle, "open", item.InstallFolder, null, null, 5); }; btnPanel.Controls.Add(btnInstallFolder); Button btnUninstall = new Button() { Text = "卸载" }; btnUninstall.Enabled = !string.IsNullOrEmpty(item.UninstallCmd); btnUninstall.Click += (o, e) => { if (Win32Import.WinExec(item.UninstallCmd, 5) > 31) { form.Close(); } else MessageBox.Show( "卸载的过程中发生异常!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); }; btnPanel.Controls.Add(btnUninstall); Button btnDel = new Button() { Text = "删除" }; btnDel.Click += (o, e) => { if (!string.IsNullOrEmpty(item.UninstallCmd) && MessageBox.Show( "检测到该软件自带卸载功能,仍然要强制从安装列表中删除项目吗?", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) return; if (MessageBox.Show("这项操作具有很高的风险,你仍然要继续吗?", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) return; using (RegistryKey k = OpenUninstallRegistryKey(true)) { k.DeleteSubKey(item.RegistryKey); form.Close(); } }; btnPanel.Controls.Add(btnDel); Button btnClose = new Button() { Text = "关闭" }; btnClose.Click += (o, e) => { form.Close(); }; btnPanel.Controls.Add(btnClose); PropertyGrid grid = new PropertyGrid(); grid.Dock = DockStyle.Fill; grid.PropertySort = PropertySort.NoSort; grid.ToolbarVisible = false; grid.SelectedObject = item; form.Controls.Add(grid); form.Controls.Add(btnPanel); // esc处理 KeyEventHandler escKeyupHandler = (o, e) => { if (e.KeyCode == Keys.Escape) form.Close(); }; form.KeyPreview = true; form.KeyUp += escKeyupHandler; form.ShowDialog(); form.Dispose(); }
static List<UinstallItem> PrepareUinstallList() { List<UinstallItem> list = new List<UinstallItem>(); using (RegistryKey uinstallKey = OpenUninstallRegistryKey(false)) { string[] subKeyNames = uinstallKey.GetSubKeyNames(); // 显示等待对话框 Form form = new Form(); form.StartPosition = FormStartPosition.CenterScreen; form.Size = new Size(250, 50); form.MinimizeBox = false; form.MaximizeBox = false; form.Text = "正在查找已经安装的软件..."; ProgressBar bar = new ProgressBar(); bar.Dock = DockStyle.Fill; bar.Maximum = 100; bar.Minimum = 0; bar.Value = bar.Minimum; form.Controls.Add(bar); BackgroundWorker worker = new BackgroundWorker(); worker.WorkerReportsProgress = true; worker.DoWork += (o, e) => { for (int i = 0; i < subKeyNames.Length; ++i) { string subKeyName = subKeyNames[i]; using (RegistryKey subKey = uinstallKey.OpenSubKey(subKeyName, false)) { UinstallItem item = new UinstallItem(); if (BuildUinstallItem(subKey, ref item)) { list.Add(item); } } worker.ReportProgress(i * 100 / subKeyNames.Length); } }; worker.ProgressChanged += (o, e) => { bar.Value = e.ProgressPercentage; }; worker.RunWorkerCompleted += (o, e) => { form.Close(); }; worker.RunWorkerAsync(); form.ShowDialog(); worker.Dispose(); form.Dispose(); } return list; }
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); }
static void TryUninstallItem(UinstallItem item) { Form form = new Form(); form.Size = new Size(350, 300); form.Text = item.DisplayName; form.StartPosition = FormStartPosition.CenterScreen; FlowLayoutPanel btnPanel = new FlowLayoutPanel(); btnPanel.Dock = DockStyle.Bottom; btnPanel.FlowDirection = FlowDirection.LeftToRight; btnPanel.BorderStyle = BorderStyle.FixedSingle; btnPanel.Height = 30; Button btnInstallFolder = new Button() { Text = "安装目录" }; btnInstallFolder.Enabled = !string.IsNullOrEmpty(item.InstallFolder); btnInstallFolder.Click += (o, e) => { Win32Import.ShellExecute(form.Handle, "open", item.InstallFolder, null, null, 5); }; btnPanel.Controls.Add(btnInstallFolder); Button btnUninstall = new Button() { Text = "卸载" }; btnUninstall.Enabled = !string.IsNullOrEmpty(item.UninstallCmd); btnUninstall.Click += (o, e) => { if (Win32Import.WinExec(item.UninstallCmd, 5) > 31) { form.Close(); } else { MessageBox.Show( "卸载的过程中发生异常!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }; btnPanel.Controls.Add(btnUninstall); Button btnDel = new Button() { Text = "删除" }; btnDel.Click += (o, e) => { if (!string.IsNullOrEmpty(item.UninstallCmd) && MessageBox.Show( "检测到该软件自带卸载功能,仍然要强制从安装列表中删除项目吗?", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } if (MessageBox.Show("这项操作具有很高的风险,你仍然要继续吗?", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } using (RegistryKey k = OpenUninstallRegistryKey(true)) { k.DeleteSubKey(item.RegistryKey); form.Close(); } }; btnPanel.Controls.Add(btnDel); Button btnClose = new Button() { Text = "关闭" }; btnClose.Click += (o, e) => { form.Close(); }; btnPanel.Controls.Add(btnClose); PropertyGrid grid = new PropertyGrid(); grid.Dock = DockStyle.Fill; grid.PropertySort = PropertySort.NoSort; grid.ToolbarVisible = false; grid.SelectedObject = item; form.Controls.Add(grid); form.Controls.Add(btnPanel); // esc处理 KeyEventHandler escKeyupHandler = (o, e) => { if (e.KeyCode == Keys.Escape) { form.Close(); } }; form.KeyPreview = true; form.KeyUp += escKeyupHandler; form.ShowDialog(); form.Dispose(); }
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)); }