C#把自写的卸载程序添加到控制面板的添加删除程序目录中

35 篇文章 0 订阅

在HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall下面创建子键,键值DisplayName是软件名称,UninstallString是卸载的命令行。你可以用注册表编辑器打开这个键,参考其它软件填写的内容。


如下阿里旺旺:


Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\阿里旺旺2013Beta2]
"DisplayName"="阿里旺旺2013Beta2"
"UninstallString"="C:\\Program Files\\AliWangWang\\Uninstall.exe"
"DisplayIcon"="C:\\Program Files\\AliWangWang\\Uninstall.exe"
"Publisher"="阿里巴巴(中国)有限公司"
"InstallLocation"=hex(2):43,00,3a,00,5c,00,50,00,72,00,6f,00,67,00,72,00,61,00,\
  6d,00,20,00,46,00,69,00,6c,00,65,00,73,00,5c,00,41,00,6c,00,69,00,57,00,61,\
  00,6e,00,67,00,57,00,61,00,6e,00,67,00,00,00
"NoModify"=dword:00000001
"NoRepair"=dword:00000001
"InstallDate"="20130912"




自写例子代码,仅供参考学习。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.IO;
using System.Threading;

namespace UninstallDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public Form1(string[] args)
        {
            InitializeComponent();
            try
            {
                if (args != null && args.Length > 0 && "uninstall".Equals(args[0].ToLower()))
                {
                    string registData;
                    RegistryKey hkml = Registry.LocalMachine;
                    RegistryKey software = hkml.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\demo", true);

                    registData = software.GetValue("DisplayIcon").ToString();
                    Thread t = new Thread(new ThreadStart(delegate
                    {
                        foreach (string aa in Directory.GetFiles(registData))
                        {
                            if (File.Exists(aa) && !aa.Equals(registData + "\\UninstallDemo.exe"))
                            {
                                File.Delete( aa);
                                this.Invoke((EventHandler)delegate
                                {
                                    textBox1.Text = "删除:" + aa + "成功?" + textBox1.Text;
                                });
                              
                                Thread.Sleep(100);
                            }
                        }
                        RegistryKey softwaredemo = hkml.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\", true);
                        softwaredemo.DeleteSubKey("demo");
                        this.Invoke((EventHandler)delegate
                        {
                            textBox1.Text = "卸载成功!" + textBox1.Text;
                        });
                    }));
                    t.Start();
                }
            }
            catch { }
        }

        private void button1_Click(object sender, EventArgs e)
        {
//            "DisplayName"="阿里旺旺2013Beta2"
//"UninstallString"="C:\\Program Files\\AliWangWang\\Uninstall.exe"
//"DisplayIcon"="C:\\Program Files\\AliWangWang\\Uninstall.exe"
            if (DialogResult.OK == folderBrowserDialog1.ShowDialog())
            {
                string aa=folderBrowserDialog1.SelectedPath;
                string registData;
                RegistryKey hkml = Registry.LocalMachine;
                RegistryKey software = hkml.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", true);
               RegistryKey softdemo= software.CreateSubKey("demo");
             //RegistryKey softname=  softdemo.CreateSubKey("DisplayName");
               softdemo.SetValue("DisplayName","测试");
               softdemo.SetValue("UninstallString", aa + "\\UninstallDemo.exe uninstall");
               softdemo.SetValue("DisplayIcon", aa);
            //RegistryKey softstring=   softdemo.CreateSubKey("UninstallString");
            // RegistryKey softicon=  softdemo.CreateSubKey("DisplayIcon");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                if (args != null && args.Length > 0 && "uninstall".Equals(args[0].ToLower()))
                {
                    string registData;
                    RegistryKey hkml = Registry.LocalMachine;
                    RegistryKey software = hkml.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\demo", true);

                    registData = software.GetValue("DisplayIcon").ToString();
                    Thread t = new Thread(new ThreadStart(delegate
                    {
                        foreach (string aa in Directory.GetFiles(registData))
                        {
                            if (File.Exists(aa) && !aa.Equals(registData + "\\UninstallDemo.exe"))
                            {
                                File.Delete(aa);
                                this.Invoke((EventHandler)delegate
                                {
                                    textBox1.Text = "删除:" + aa + "成功?" + textBox1.Text;
                                });

                                Thread.Sleep(100);
                            }
                        }
                        RegistryKey softwaredemo = hkml.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\", true);
                        softwaredemo.DeleteSubKey("demo");
                        this.Invoke((EventHandler)delegate
                        {
                            textBox1.Text = "卸载成功!" + textBox1.Text;
                        });
                    }));
                    t.Start();
                }
            }
            catch { }
        }
    }
}


  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值