欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

VB.NET中調(diào)用MSI卸載軟件的2個(gè)方法

 更新時(shí)間:2014年07月16日 10:57:52   投稿:junjie  
這篇文章主要介紹了VB.NET中調(diào)用MSI卸載軟件的2個(gè)方法,一是直接調(diào)用MSI安裝包命令,二是產(chǎn)品序列號卸載程序,需要的朋友可以參考下

最近在折騰組里面的那個(gè)破Lab,要自己寫程序每天安裝最新版本的build。而今天手頭上沒有任何任務(wù),所以把用到的一些東西記下來以供今后參考。這篇日志來記錄如何在.NET中卸載別的軟件。

一、直接使用MSI安裝包

  如果你知道MSI安裝程序的路徑,那么顯然可以直接使用即可:

復(fù)制代碼 代碼如下:

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

/quiet參數(shù)表示自動卸載,/qn表示 顯示任何UI。

  這個(gè)方法很簡單,推薦使用。但是如果軟件的版本不對,或者安裝程序做得有問題(比如我們這做的一個(gè)奇葩安裝程序),那么就不行了。

復(fù)制代碼 代碼如下:

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

 但是這個(gè)序列號是不定的,對于相同程序的不同版本,序列號也不一定相同(可能會生成一個(gè)新的序列號)。為了得到需要產(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)的在安裝時(shí)不共享的那些程序。

 如上圖,在Products鍵下有一大堆十六進(jìn)制的數(shù)字。在數(shù)字下可能有InstallProperties子鍵(注意不是每一個(gè)都有),然后有DisplayName用于標(biāo)識產(chǎn)品的名稱,DisplayVersion用于顯示版本號等等。我們只需要關(guān)注會用到的就行了,這里就只關(guān)注產(chǎn)品名稱吧。

在左側(cè)顯示的數(shù)字并不是用于msi卸載的產(chǎn)品序列號。我們注意到有一個(gè)UninstallString屬性,這個(gè)屬性就是卸載這個(gè)程序的命令及參數(shù):

復(fù)制代碼 代碼如下:

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

那么,我們要做的很顯然是搜索這些鍵,查找哪一個(gè)才是我們要卸載的產(chǎn)品,然后用UninstallString把它卸掉就行了。另外我們需要在參數(shù)上加上/quiet和/qn參數(shù),這樣就能實(shí)現(xiàn)自動卸載了。

復(fù)制代碼 代碼如下:

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類似樹形結(jié)構(gòu))。

  這就是實(shí)現(xiàn)自動卸載的代碼(里面有一些與輸出日志相關(guān)的代碼,可以不用管它)。

  程序首先在Products鍵下搜索所有的產(chǎn)品,如果有InstallProperties子鍵,就匹配DisplayName是否與要卸載的程序相同,如果相同,就生成一個(gè)卸載的命令并啟動一個(gè)新的進(jìn)程進(jìn)行卸載。

  如果卸載失敗,msiexec會返回一個(gè)不為0的數(shù)值,此時(shí)我們將錯(cuò)誤信息輸出。(注意:還有兩個(gè)數(shù)值表示卸載成功但是需要重啟,請自行查找相關(guān)手冊。)

相關(guān)文章

最新評論