c# msiexec.exe卸载软件,cmd命令REG DELETE 清除注册表实例
2021-04-02 21:25
标签:查看 ret test msi file string pre abi tin cmd执行关键代码 卸载软件:msiexec.exe /x {xxxxx-xxxx-xxxx-xxxxx} /quiet /norestart 解释: {xxxxx-xxxx-xxxx-xxxxx} 软件productcode /quiet 安静模式,无用户交互 /norestart 安装完成后不重新启动 清理注册表:REG DELETE "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\xxxxxxx" /f 解释: /f 不用提示就强行删除 cmd命令查看帮助 界面 CmdHelper.cs Form1.cs Form1.Designer.cs c# msiexec.exe卸载软件,cmd命令REG DELETE 清除注册表实例 标签:查看 ret test msi file string pre abi tin 原文地址:https://www.cnblogs.com/a735882640/p/9211818.htmlusing System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
namespace UninstallTest
{
public class CmdHelper
{
private static string CmdPath = @"C:\Windows\System32\cmd.exe";
///
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace UninstallTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Load += Form1_Load;
}
#region 事件
void Form1_Load(object sender, EventArgs e)
{
this.textBox1.Text = "upos";
}
private void button1_Click(object sender, EventArgs e)
{
this.richTextBox1.Text = "";
this.richTextBox2.Text = "";
var msg = "";
try
{
var input = this.textBox1.Text;
if (string.IsNullOrEmpty(input))
{
throw new Exception("请输入productcode");
}
var urls = new Liststring>();
var result = GetAllProductCode(input, urls);
msg = string.Join("\r\n", result.ToArray());
if (string.IsNullOrEmpty(msg))
{
throw new Exception("未找到productCode");
}
this.richTextBox2.Text = string.Join("\r\n", urls.ToArray());
}
catch (Exception ex)
{
msg = ex.Message;
}
this.richTextBox1.Text = msg;
}
private void button2_Click(object sender, EventArgs e)
{
var msg = "";
try
{
//卸载
var productcodes = this.richTextBox1.Text;
var productcodelst = productcodes.Split(new string[] { "\r", "\n", "," }, StringSplitOptions.RemoveEmptyEntries);
foreach (var item in productcodelst)
{
using (Process p = new Process())
{
p.StartInfo.FileName = "msiexec.exe";
p.StartInfo.Arguments = "/x {" + item + "} /quiet /norestart";
p.Start();
p.WaitForExit();
}
}
//清理注册表
var urls = this.richTextBox2.Text;
var urllst = urls.Split(new string[] { "\r", "\n", "," }, StringSplitOptions.RemoveEmptyEntries);
foreach (var item in urllst)
{
var result = "";
var cmd = "REG DELETE \"" + item + "\" /f";
CmdHelper.RunCmd(cmd, out result);
}
msg = "已完成";
}
catch (Exception ex)
{
msg = ex.Message;
}
this.richTextBox1.Text = msg;
}
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.A)
{
(sender as RichTextBox).SelectAll();
}
}
#endregion
#region 方法
public static Liststring> GetAllProductCode(string displayName, Liststring> urls)
{
var result = new Liststring>();
urls.Clear();
// 如果是32位操作系统,(或者系统是64位,程序也是64位)
string bit32 = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
// 如果操作系统是64位并且程序是32位的
string bit64 = @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall";
RegistryKey localMachine = Registry.LocalMachine;
//1
RegistryKey Uninstall32 = localMachine.OpenSubKey(bit32, true);
foreach (string subkey in Uninstall32.GetSubKeyNames())
{
RegistryKey productcode = Uninstall32.OpenSubKey(subkey);
try
{
string displayname = productcode.GetValue("DisplayName").ToString();
if (displayname.ToLower().Contains(displayName.ToLower()))
{
urls.Add(productcode.ToString());
string productCode = string.Empty;
string uninstallString = productcode.GetValue("UninstallString").ToString();
string[] strs = uninstallString.Split(new char[2] { ‘{‘, ‘}‘ });
productCode = strs[1];
result.Add(productCode);
}
}
catch { }
}
//2
RegistryKey Uninstall64 = localMachine.OpenSubKey(bit64, true);
foreach (string subkey in Uninstall64.GetSubKeyNames())
{
RegistryKey productcode = Uninstall64.OpenSubKey(subkey);
try
{
string displayname = productcode.GetValue("DisplayName").ToString();
if (displayname.ToLower().Contains(displayName.ToLower()))
{
urls.Add(productcode.ToString());
string productCode = string.Empty;
string uninstallString = productcode.GetValue("UninstallString").ToString();
string[] strs = uninstallString.Split(new char[2] { ‘{‘, ‘}‘ });
productCode = strs[1];
result.Add(productCode);
}
}
catch { }
}
return result;
}
#endregion
}
}
namespace UninstallTest
{
partial class Form1
{
///
上一篇:C# 抽象
下一篇:背水一战 Windows 10 (122) - 其它: 通过 Windows.System.Profile 命名空间下的类获取信息, 查找指定类或接口的所在程序集的所有子类和子接口
文章标题:c# msiexec.exe卸载软件,cmd命令REG DELETE 清除注册表实例
文章链接:http://soscw.com/index.php/essay/71547.html