首页 > 代码库 > C#卸载某个应用软件

C#卸载某个应用软件

 1 using Microsoft.Win32; 2 using System; 3 using System.Collections.Generic; 4 using System.Diagnostics; 5 using System.Linq; 6 using System.Text; 7 using System.Threading.Tasks; 8  9 namespace UpdateModule10 {11     class SoftUnInstall12     {13         //static void Main(string[] args)14         //{15 16         //    UnInstall();17  18         //}19 20         //获取软件的ProductCode,卸载时候使用21         public static string GetProductCode(string displayName)22         {23             string productCode = string.Empty;24 25             // 如果是32位操作系统,(或者系统是64位,程序也是64位)26             string bit32 = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";27             // 如果操作系统是64位并且程序是32位的28             string bit64 = @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall";29 30             RegistryKey localMachine = Registry.LocalMachine;31             RegistryKey Uninstall = localMachine.OpenSubKey(bit32, true);32 33             foreach (string subkey in Uninstall.GetSubKeyNames())34             {35                 RegistryKey productcode = Uninstall.OpenSubKey(subkey);36                 try37                 {38                     string displayname = productcode.GetValue("DisplayName").ToString();39                     if (displayname == displayName)40                     {41                         string uninstallString = productcode.GetValue("UninstallString").ToString();42 43                         string[] strs = uninstallString.Split(new char[2] { {, } });44                         productCode = strs[1];45                         return productCode;46                     }47                 }48                 catch { }49             }50 51 52             return productCode;53         }54 55         //卸载软件函数,只需要卸载终端,无须卸载36056         public static void UnInstall() 57         {58             Process p = new Process();59             p.StartInfo.FileName = "msiexec.exe";60             string str1 = GetProductCode("SecurityManager");   61             p.StartInfo.Arguments = "/x {" + str1 + "} /quiet /norestart";62             p.Start();63         }64 65     }66 }

 

C#卸载某个应用软件