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

转自: http://blog.csdn.net/liujun198773/article/details/17244337


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


如下阿里旺旺:


[plain]  view plain  copy
  1. Windows Registry Editor Version 5.00  
  2.   
  3. [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\阿里旺旺2013Beta2]  
  4. "DisplayName"="阿里旺旺2013Beta2"  
  5. "UninstallString"="C:\\Program Files\\AliWangWang\\Uninstall.exe"  
  6. "DisplayIcon"="C:\\Program Files\\AliWangWang\\Uninstall.exe"  
  7. "Publisher"="阿里巴巴(中国)有限公司"  
  8. "InstallLocation"=hex(2):43,00,3a,00,5c,00,50,00,72,00,6f,00,67,00,72,00,61,00,\  
  9.   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,\  
  10.   00,6e,00,67,00,57,00,61,00,6e,00,67,00,00,00  
  11. "NoModify"=dword:00000001  
  12. "NoRepair"=dword:00000001  
  13. "InstallDate"="20130912"  




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

[csharp]  view plain  copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using Microsoft.Win32;  
  10. using System.IO;  
  11. using System.Threading;  
  12.   
  13. namespace UninstallDemo  
  14. {  
  15.     public partial class Form1 : Form  
  16.     {  
  17.         public Form1()  
  18.         {  
  19.             InitializeComponent();  
  20.         }  
  21.         public Form1(string[] args)  
  22.         {  
  23.             InitializeComponent();  
  24.             try  
  25.             {  
  26.                 if (args != null && args.Length > 0 && "uninstall".Equals(args[0].ToLower()))  
  27.                 {  
  28.                     string registData;  
  29.                     RegistryKey hkml = Registry.LocalMachine;  
  30.                     RegistryKey software = hkml.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\demo"true);  
  31.   
  32.                     registData = software.GetValue("DisplayIcon").ToString();  
  33.                     Thread t = new Thread(new ThreadStart(delegate  
  34.                     {  
  35.                         foreach (string aa in Directory.GetFiles(registData))  
  36.                         {  
  37.                             if (File.Exists(aa) && !aa.Equals(registData + "\\UninstallDemo.exe"))  
  38.                             {  
  39.                                 File.Delete( aa);  
  40.                                 this.Invoke((EventHandler)delegate  
  41.                                 {  
  42.                                     textBox1.Text = "删除:" + aa + "成功?" + textBox1.Text;  
  43.                                 });  
  44.                                 
  45.                                 Thread.Sleep(100);  
  46.                             }  
  47.                         }  
  48.                         RegistryKey softwaredemo = hkml.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"true);  
  49.                         softwaredemo.DeleteSubKey("demo");  
  50.                         this.Invoke((EventHandler)delegate  
  51.                         {  
  52.                             textBox1.Text = "卸载成功!" + textBox1.Text;  
  53.                         });  
  54.                     }));  
  55.                     t.Start();  
  56.                 }  
  57.             }  
  58.             catch { }  
  59.         }  
  60.   
  61.         private void button1_Click(object sender, EventArgs e)  
  62.         {  
  63. //            "DisplayName"="阿里旺旺2013Beta2"  
  64. //"UninstallString"="C:\\Program Files\\AliWangWang\\Uninstall.exe"  
  65. //"DisplayIcon"="C:\\Program Files\\AliWangWang\\Uninstall.exe"  
  66.             if (DialogResult.OK == folderBrowserDialog1.ShowDialog())  
  67.             {  
  68.                 string aa=folderBrowserDialog1.SelectedPath;  
  69.                 string registData;  
  70.                 RegistryKey hkml = Registry.LocalMachine;  
  71.                 RegistryKey software = hkml.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"true);  
  72.                RegistryKey softdemo= software.CreateSubKey("demo");  
  73.              //RegistryKey softname=  softdemo.CreateSubKey("DisplayName");  
  74.                softdemo.SetValue("DisplayName","测试");  
  75.                softdemo.SetValue("UninstallString", aa + "\\UninstallDemo.exe uninstall");  
  76.                softdemo.SetValue("DisplayIcon", aa);  
  77.             //RegistryKey softstring=   softdemo.CreateSubKey("UninstallString");  
  78.             // RegistryKey softicon=  softdemo.CreateSubKey("DisplayIcon");  
  79.             }  
  80.         }  
  81.   
  82.         private void button2_Click(object sender, EventArgs e)  
  83.         {  
  84.             try  
  85.             {  
  86.                 if (args != null && args.Length > 0 && "uninstall".Equals(args[0].ToLower()))  
  87.                 {  
  88.                     string registData;  
  89.                     RegistryKey hkml = Registry.LocalMachine;  
  90.                     RegistryKey software = hkml.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\demo"true);  
  91.   
  92.                     registData = software.GetValue("DisplayIcon").ToString();  
  93.                     Thread t = new Thread(new ThreadStart(delegate  
  94.                     {  
  95.                         foreach (string aa in Directory.GetFiles(registData))  
  96.                         {  
  97.                             if (File.Exists(aa) && !aa.Equals(registData + "\\UninstallDemo.exe"))  
  98.                             {  
  99.                                 File.Delete(aa);  
  100.                                 this.Invoke((EventHandler)delegate  
  101.                                 {  
  102.                                     textBox1.Text = "删除:" + aa + "成功?" + textBox1.Text;  
  103.                                 });  
  104.   
  105.                                 Thread.Sleep(100);  
  106.                             }  
  107.                         }  
  108.                         RegistryKey softwaredemo = hkml.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"true);  
  109.                         softwaredemo.DeleteSubKey("demo");  
  110.                         this.Invoke((EventHandler)delegate  
  111.                         {  
  112.                             textBox1.Text = "卸载成功!" + textBox1.Text;  
  113.                         });  
  114.                     }));  
  115.                     t.Start();  
  116.                 }  
  117.             }  
  118.             catch { }  
  119.         }  
  120.     }  
  121. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值