c# msiexec.exe卸载软件,cmd命令REG DELETE 清除注册表实例

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

using 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";

        /// <summary>
        /// 执行cmd命令
        /// 多命令请使用批处理命令连接符:
        /// <![CDATA[
        /// &:同时执行两个命令
        /// |:将上一个命令的输出,作为下一个命令的输入
        /// &&:当&&前的命令成功时,才执行&&后的命令
        /// ||:当||前的命令失败时,才执行||后的命令]]>
        /// 其他请百度
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="output"></param>
        public static void RunCmd(string cmd, out string output)
        {
            cmd = cmd.Trim().TrimEnd(‘&‘) + "&exit";//说明:不管命令是否成功均执行exit命令,否则当调用ReadToEnd()方法时,会处于假死状态
            using (Process p = new Process())
            {
                p.StartInfo.FileName = CmdPath;
                p.StartInfo.UseShellExecute = false;        //是否使用操作系统shell启动
                p.StartInfo.RedirectStandardInput = true;   //接受来自调用程序的输入信息
                p.StartInfo.RedirectStandardOutput = true;  //由调用程序获取输出信息
                p.StartInfo.RedirectStandardError = true;   //重定向标准错误输出
                p.StartInfo.CreateNoWindow = true;          //不显示程序窗口
                p.Start();//启动程序

                //向cmd窗口写入命令
                p.StandardInput.WriteLine(cmd);
                p.StandardInput.AutoFlush = true;

                //获取cmd窗口的输出信息
                output = p.StandardOutput.ReadToEnd();
                p.WaitForExit();//等待程序执行完退出进程
                p.Close();
            }
        }
    }
}

Form1.cs

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 List<string>();
                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 List<string> GetAllProductCode(string displayName, List<string> urls)
        {
            var result = new List<string>();
            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
    }
}

Form1.Designer.cs

namespace UninstallTest
{
    partial class Form1
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.richTextBox1 = new System.Windows.Forms.RichTextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.button2 = new System.Windows.Forms.Button();
            this.richTextBox2 = new System.Windows.Forms.RichTextBox();
            this.label4 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            //
            // button1
            //
            this.button1.Location = new System.Drawing.Point(578, 45);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(88, 39);
            this.button1.TabIndex = 0;
            this.button1.Text = "查询productCode";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            //
            // textBox1
            //
            this.textBox1.Location = new System.Drawing.Point(102, 45);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(229, 21);
            this.textBox1.TabIndex = 1;
            //
            // richTextBox1
            //
            this.richTextBox1.Location = new System.Drawing.Point(102, 89);
            this.richTextBox1.Name = "richTextBox1";
            this.richTextBox1.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.ForcedVertical;
            this.richTextBox1.Size = new System.Drawing.Size(438, 120);
            this.richTextBox1.TabIndex = 2;
            this.richTextBox1.Text = "";
            this.richTextBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.richTextBox1_KeyDown);
            //
            // label1
            //
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(23, 45);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(65, 12);
            this.label1.TabIndex = 3;
            this.label1.Text = "程序名称:";
            //
            // label2
            //
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(23, 123);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(41, 12);
            this.label2.TabIndex = 4;
            this.label2.Text = "结果:";
            //
            // button2
            //
            this.button2.Location = new System.Drawing.Point(578, 107);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(88, 43);
            this.button2.TabIndex = 5;
            this.button2.Text = "卸载软件";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            //
            // richTextBox2
            //
            this.richTextBox2.Location = new System.Drawing.Point(102, 230);
            this.richTextBox2.Name = "richTextBox2";
            this.richTextBox2.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.ForcedVertical;
            this.richTextBox2.Size = new System.Drawing.Size(438, 120);
            this.richTextBox2.TabIndex = 2;
            this.richTextBox2.Text = "";
            this.richTextBox2.KeyDown += new System.Windows.Forms.KeyEventHandler(this.richTextBox1_KeyDown);
            //
            // label4
            //
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(23, 233);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(35, 12);
            this.label4.TabIndex = 4;
            this.label4.Text = "url:";
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(733, 413);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.richTextBox2);
            this.Controls.Add(this.richTextBox1);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "卸载软件/清理注册表";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.RichTextBox richTextBox1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.RichTextBox richTextBox2;
        private System.Windows.Forms.Label label4;
    }
}

原文地址:https://www.cnblogs.com/a735882640/p/9211818.html

时间: 2024-10-13 21:16:08

c# msiexec.exe卸载软件,cmd命令REG DELETE 清除注册表实例的相关文章

C++ 执行 cmd 命令 删除文件 删除注册表项

#include <Windows.h> WinExec("cmd /C \"del C:\\Windows\\secretWin.ini\"",SW_HIDE); WinExec("cmd /C \"del C:\\Windows\\simpleTrboUpdateInfo.ini\"",SW_HIDE); WinExec("cmd /C \"echo Y | reg delete HKEY_

yum安装于卸载软件常见命令

1.使用yum安装和卸载软件,有个前提是yum安装的软件包都是rpm格式的. 安装的命令是,yuminstall ~,yum会查询数据库,有无这一软件包,如果有,则检查其依赖冲突关系,如果没有依赖冲突,那么最好,下载安装;如果有,则会给出提示,询问是否要同时安装依赖,或删除冲突的包,你可以自己作出判断:        删除的命令是,yum remove ~,同安装一样,yum也会查询数据库,给出解决依赖关系的提示. 其中~代表软件名 1.用YUM安装软件包命令:yuminstall ~ 2.用Y

yum安装与卸载软件常见命令

1.使用yum安装和卸载软件,有个前提是yum安装的软件包都是rpm格式的. 安装的命令是,yuminstall ~,yum会查询数据库,有无这一软件包,如果有,则检查其依赖冲突关系,如果没有依赖冲突,那么最好,下载安装;如果有,则会给出提示,询问是否要同时安装依赖,或删除冲突的包,你可以自己作出判断:删除的命令是,yum remove ~,同安装一样,yum也会查询数据库,给出解决依赖关系的提示. 其中~代表软件名 1.用YUM安装软件包命令:yuminstall ~ 2.用YUM删除软件包命

鼠标右键添加 打开命令行窗口(管理员) 注册表

Windows Registry Editor Version 5.00 [-HKEY_CLASSES_ROOT\Directory\shell\runas] [HKEY_CLASSES_ROOT\Directory\shell\runas] @="打开命令行窗口(管理员)" "HasLUAShield"="" [HKEY_CLASSES_ROOT\Directory\shell\runas\command] @="cmd.exe /s

cmd中删除、添加、修改注册表命令

转自:http://www.jb51.net/article/30586.htm regedit的运行参数 REGEDIT [/L:system] [/R:user] filename1 REGEDIT [/L:system] [/R:user] /C filename2 REGEDIT [/L:system] [/R:user] /E filename3 [regpath] /L:system Specifies the location of the SYSTEM.DAT file. /R:

REG Delete用法

reg delete 从注册表删除项或子项 语法:reg delete KeyName [{/v EntryName|/ve|/va}] [/f] 参数 KeyName 指定子项的完全路径.对于远程计算机,请在\ComputerName\PathToSubkey中的子项路径前包含计算机名称.忽略ComputerName会导致默认对本地计算机进行操作.以相应的子目录树开始路径.有效子目录树为HKLM.HKCU.HKCR.HKU以及HKCC. /v EntryName 删除子项下的特定项.如果未指定

CMD命令:开始->运行->键入cmd或command(在命令行里可以看到系统版本、文件系统版本)

CMD命令 CMD命令:开始->运行->键入cmd或command(在命令行里可以看到系统版本.文件系统版本) appwiz.cpl:程序和功能 calc:启动计算器 certmgr.msc:证书管理实用程序 charmap:启动字符映射表 chkdsk.exe:Chkdsk磁盘检查(管理员身份运行命令提示符) cleanmgr: 打开磁盘清理工具 cliconfg:SQL SERVER 客户端网络实用工具 cmstp:连接管理器配置文件安装程序 cmd.exe:CMD命令提示符 自动关机命令

最全DOS的CMD命令,程序员必会

CMD命令:开始->运行->键入cmd或command(在命令行里可以看到系统版本.文件系统版本)1. appwiz.cpl:程序和功能 2. calc:启动计算器 3. certmgr.msc:证书管理实用程序 4. charmap:启动字符映射表 5. chkdsk.exe:Chkdsk磁盘检查(管理员身份运行命令提示符) 6. cleanmgr: 打开磁盘清理工具 7. cliconfg:SQL SERVER 客户端网络实用工具 8. cmstp:连接管理器配置文件安装程序 9. cmd

最全的CMD命令

CMD命令:开始->运行->键入cmd或command(在命令行里可以看到系统版本.文件系统版本) 1. appwiz.cpl:程序和功能 2. calc:启动计算器 3. certmgr.msc:证书管理实用程序 4. charmap:启动字符映射表 5. chkdsk.exe:Chkdsk磁盘检查(管理员身份运行命令提示符) 6. cleanmgr: 打开磁盘清理工具 7. cliconfg:SQL SERVER 客户端网络实用工具 8. cmstp:连接管理器配置文件安装程序 9. cm