程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> Visual Basic語言 >> VB.NET >> VB.NET中挪用MSI卸載軟件的2個辦法

VB.NET中挪用MSI卸載軟件的2個辦法

編輯:VB.NET

VB.NET中挪用MSI卸載軟件的2個辦法。本站提示廣大學習愛好者:(VB.NET中挪用MSI卸載軟件的2個辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是VB.NET中挪用MSI卸載軟件的2個辦法正文


比來在折騰組外面的誰人破Lab,要本身寫法式天天裝置最新版本的build。而明天手頭上沒有任何義務,所以把用到的一些器械記上去以供往後參考。這篇日記來記載若何在.NET中卸載其余軟件。

1、直接應用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

 然則這個序列號是不定的,關於雷同法式的分歧版本,序列號也紛歧定雷同(能夠會生成一個新的序列號)。為了獲得須要產物的序列號,就只能去查注冊表了。

2、應用產物序列號卸載法式

  一切用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.WordStr("msiexec.exe", "").WordStr("msiexec", "").ToUpper
 
                uninstall_string = uninstall_string.WordStr("/I", "/x")
                uninstall_string = uninstall_string.WordStr("/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的數值,此時我們將毛病信息輸入。(留意:還有兩個數值表現卸載勝利然則須要重啟,請自行查找相干手冊。)

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved