最近在折騰組里面的那個破Lab,要自己寫程序每天安裝最新版本的build。而今天手頭上沒有任何任務(wù),所以把用到的一些東西記下來以供今后參考。這篇日志來記錄如何在.NET中卸載別的軟件。
一、直接使用MSI安裝包
如果你知道MSI安裝程序的路徑,那么顯然可以直接使用即可:
msiexec /x "C:Table Manager Clients.msi" /quiet /qn
/quiet參數(shù)表示自動卸載,/qn表示 顯示任何UI。
這個方法很簡單,推薦使用。但是如果軟件的版本不對,或者安裝程序做得有問題(比如我們這做的一個奇葩安裝程序),那么就不行了。
msiexec /Option
[Optional Parameter]
Install Options
Installs or configures a product
但是這個序列號是不定的,對于相同程序的不同版本,序列號也不一定相同(可能會生成一個新的序列號)。為了得到需要產(chǎn)品的序列號,就只能去查注冊表了。
二、使用產(chǎn)品序列號卸載程序
所有用MSI安裝的程序都會記錄在HKEY_LOCAL_MACHINE的SOFTWAREMicrosoftWindowsCurrentVersionInstallerUserDataS-1-5-18Products子健下。S-1-5-18是系統(tǒng)通用的用戶,可能有其他的用戶目錄(比如我這有S-1-5-21-2109753547-707883502-1543859470-98763),應(yīng)該是對應(yīng)的在安裝時不共享的那些程序。
![](/biancheng/files/2017-7/5/1654358746.jpg?2014616105836)
如上圖,在Products鍵下有一大堆十六進(jìn)制的數(shù)字。在數(shù)字下可能有InstallProperties子鍵(注意不是每一個都有),然后有DisplayName用于標(biāo)識產(chǎn)品的名稱,DisplayVersion用于顯示版本號等等。我們只需要關(guān)注會用到的就行了,這里就只關(guān)注產(chǎn)品名稱吧。
在左側(cè)顯示的數(shù)字并不是用于msi卸載的產(chǎn)品序列號。我們注意到有一個UninstallString屬性,這個屬性就是卸載這個程序的命令及參數(shù):
MsiExec.exe /X{4B9E6EB0-0EED-4E74-9479-F982C3254F71}
那么,我們要做的很顯然是搜索這些鍵,查找哪一個才是我們要卸載的產(chǎn)品,然后用UninstallString把它卸掉就行了。另外我們需要在參數(shù)上加上/quiet和/qn參數(shù),這樣就能實現(xiàn)自動卸載了。
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