C# 自动卸载软件(转)
1.用msiexec 命令来卸载软件
平常我们手动卸载软件都是到控制面板中的"添加/删除"程序中去卸载软件, 或者通过程序自带的卸载软件来卸载。
我们可以通过 MsiExec.exe /X{ProductCode} 命令来卸载程序。
关于MsiExec.exe 请看 http://technet.microsoft.com/zh-cn/library/cc759262%28v=WS.10%29.aspx
2.注册表中查找ProductCode
ProductCode是Windows 安装程序包的全局唯一标识符 (GUID), 我们可以通过注册表来获取ProductCode
实例: 用MsiExec.exe 自动卸载Xmarks.
Xmarks 是一个用来同步收藏夹的工具, 我平常用来同步IE,firefox,chrome的收藏夹。
先用注册表打开如下位置,
32位操作系统: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\
注意: 如果是64位操作系统:
64位的程序还在: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\
32位的程序而是在: HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\
Uninstall下面的注册表子键很多, 你需要耐心地一个一个去查找"DisplayName", 从而找到程序的ProductCode, 如下图。

从注册表中我们找到UninstallString这个键值: MsiExec.exe /X{C56BBAC8-0DD2-4CE4-86E0-F2BDEABDD0CF}, 那么ProductCode就是{C56BBAC8-0DD2-4CE4-86E0-F2BDEABDD0CF}
我们可以通过 MsiExec.exe /X{ProductCode} 命令来卸载程序.
那么卸载的命令应该为 MsiExec.exe /X{C56BBAC8-0DD2-4CE4-86E0-F2BDEABDD0CF}
然后在CMD中直接调用这个命令, 会弹出一个对话框,点击"是" 后, 软件就能被卸载了。

在自动化测试中,我们不想弹出这个对话框,而是希望直接卸载。同时也不希望系统重启 只要加个两个参数 /quiet /norestart 就可以了
现在的卸载的命令是: MsiExec.exe /X{C56BBAC8-0DD2-4CE4-86E0-F2BDEABDD0CF} /quiet
3.C#中卸载程序
C#的卸载代码比较简单, 当然你也可以用其他语言。
            Process p = new Process();
            p.StartInfo.FileName = "msiexec.exe";
            p.StartInfo.Arguments = "/x {C56BBAC8-0DD2-4CE4-86E0-F2BDEABDD0CF} /quiet /norestart";
            p.Start(); 
4.C#查找注册表中的ProductCode
这里面要注意:有的程序的卸载UninstallString 是程序文件本身文件夹下的uninstall.exe,有的则是通过MsiExec.exe,要注意区分
以下代码是通过MsiExec.exe进行卸载程序
   public static string GetProductGuid(string displayName)
      {
         string productGuid = string.Empty;
         string bit32 = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
         RegistryKey localMachine = Registry.LocalMachine;
         RegistryKey unistall = localMachine.OpenSubKey(bit32, true);
         var subNames = unistall.GetSubKeyNames();
         foreach (string subkey in subNames)
         {
            RegistryKey product = unistall.OpenSubKey(subkey);
            try
            {
               if (product.GetValueNames().Any(n => n == "DisplayName") == true)
               {
                  string tempDisplayName = product.GetValue("DisplayName").ToString();
                  if (tempDisplayName == displayName && product.GetValueNames().Any(n => n == "UninstallString") == true)
                  {
                     var unitstallStr = product.GetValue("UninstallString").ToString();
                     if (unitstallStr.Contains("MsiExec.exe"))
                     {
                        string[] strs = unitstallStr.Split(new char[2] { '{', '}' });
                        productGuid = strs[1];
                        break;
                     }
                  }
               }
            }
            catch
            {
               return string.Empty;
            }
         }
         return productGuid;
      }
参考:http://www.cnblogs.com/TankXiao/archive/2012/10/18/2727072.html#msiexec
                    
                
                
            
        
浙公网安备 33010602011771号