程序中调用MSI卸载软件的方法

  最近在折腾组里面的那个破Lab,要自己写程序每天安装最新版本的build。而今天手头上没有任何任务,所以把用到的一些东西记下来以供今后参考(其实最重要的是凑本月博文的数量,玩游戏去了根本没时间来写日志)。这篇日志来记录如何在.NET中卸载别的软件吧。


一、直接使用MSI安装包

  如果你知道MSI安装程序的路径,那么显然可以直接使用即可:

msiexec /x "C:Table Manager Clients.msi" /quiet /qn

  /quiet参数表示自动卸载,/qn表示 显示任何UI。

  这个方法很简单,推荐使用。但是如果软件的版本不对,或者安装程序做得有问题(比如我们这做的一个奇葩安装程序),那么就不行了。

msiexec /Option <Required Parameter> [Optional Parameter]
   
Install Options
    </package | /i> <Product.msi>
        Installs or configures a product

  但是这个序列号是不定的,对于相同程序的不同版本,序列号也不一定相同(可能会生成一个新的序列号)。为了得到需要产品的序列号,就只能去查注册表了。


二、使用产品序列号卸载程序

  所有用MSI安装的程序都会记录在HKEY_LOCAL_MACHINE的SOFTWAREMicrosoftWindowsCurrentVersionInstallerUserDataS-1-5-18Products子健下。S-1-5-18是系统通用的用户,可能有其他的用户目录(比如我这有S-1-5-21-2109753547-707883502-1543859470-98763),应该是对应的在安装时不共享的那些程序。

  如上图,在Products键下有一大堆十六进制的数字。在数字下可能有InstallProperties子键(注意不是每一个都有),然后有DisplayName用于标识产品的名称,DisplayVersion用于显示版本号等等。我们只需要关注会用到的就行了,这里就只关注产品名称吧。

  在左侧显示的数字并不是用于msi卸载的产品序列号。我们注意到有一个UninstallString属性,这个属性就是卸载这个程序的命令及参数:

MsiExec.exe /X{4B9E6EB0-0EED-4E74-9479-F982C3254F71}

   那么,我们要做的很显然是搜索这些键,查找哪一个才是我们要卸载的产品,然后用UninstallString把它卸掉就行了。另外我们需要在参数上加上/quiet和/qn参数,这样就能实现自动卸载了。

Private Sub UninstallMsi(productName As String)
    Dim reg_path As String = "SOFTWAREMicrosoftWindowsCurrentVersionInstallerUserDataS-1-5-18Products"
 
    Dim key As RegistryKey = Registry.LocalMachine.OpenSubKey(reg_path)
 
    For Each temp_key_name As String In key.GetSubKeyNames()
        Dim temp_key As RegistryKey = key.OpenSubKey(temp_key_name & "InstallProperties")
 
        If temp_key IsNot Nothing Then
            If temp_key.GetValue("DisplayName").ToString.Trim.ToLower = productName.Trim.ToLower Then
 
                Dim uninstall_string As String = temp_key.GetValue("UninstallString").ToString
 
                uninstall_string = uninstall_string.ToLower.Replace("msiexec.exe", "").Replace("msiexec", "").ToUpper
 
                uninstall_string = uninstall_string.Replace("/I", "/x")
                uninstall_string = uninstall_string.Replace("/i", "/x")
 
                uninstall_string &= " /quiet /qn"
 
                Console.WriteLine("Uninstalling product " & uninstall_string)
                LogDataAccess.InsertApplicationLog(TMConfigrationManager.GetConfig("ServerType"), _
                                                 "Uninstalling " & productName & """" & uninstall_string & """ ...")
 
                Dim proc_start_info As New ProcessStartInfo("msiexec", uninstall_string)
 
                Dim proc As Process = Process.Start(proc_start_info)
 
                If (proc IsNot Nothing) Then proc.WaitForExit()
                If proc.ExitCode <> 0 Then
                    Dim err_message As String = "Uninstall " & productName & " failed."

                    LogDataAccess.InsertApplicationLog(TMConfigrationManager.GetConfig("ServerType"), err_message)
                    Console.WriteLine(err_message)
                End If
 
                LogDataAccess.InsertApplicationLog(TMConfigrationManager.GetConfig("ServerType"), "Uninstall previous version of " & productName & " successful.")
 
                Exit Sub
            End If
        End If
    Next
 
    Dim message As String = "Cannot find " & productName & " registry entries. Do not need to uninstall."

    LogDataAccess.InsertApplicationLog(TMConfigrationManager.GetConfig("ServerType"), message)
    Console.WriteLine(message)
End Sub

  .NET中访问注册表的类封装在Microsoft.Win32命名空间下,直接使用即可(主要使用RegistryKey类,RegisitryKey类似树形结构)。

  这就是实现自动卸载的代码(里面有一些与输出日志相关的代码,可以不用管它)。

  程序首先在Products键下搜索所有的产品,如果有InstallProperties子键,就匹配DisplayName是否与要卸载的程序相同,如果相同,就生成一个卸载的命令并启动一个新的进程进行卸载。

  如果卸载失败,msiexec会返回一个不为0的数值,此时我们将错误信息输出。(注意:还有两个数值表示卸载成功但是需要重启,请自行查找相关手册。)

✏️ 有任何想法?欢迎发邮件告诉老夫:daozhihun@outlook.com