最近在折騰組里面的那個破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的數值,此時我們將錯誤信息輸出。(注意:還有兩個數值表示卸載成功但是需要重啟,請自行查找相關手冊。)