C#把自写的卸载程序添加到控制面板的添加删除程序目录中
转自: http://blog.****.net/liujun198773/article/details/17244337
- 在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 { }
- }
- }
- }