亚洲欧洲国产欧美一区精品,激情五月亚洲色五月,最新精品国偷自产在线婷婷,欧美婷婷丁香五月天社区

      考試首頁 | 考試用書 | 培訓(xùn)課程 | 模擬考場 | 考試論壇  
        當(dāng)前位置:編程開發(fā) > DotNET > VB.Net > 文章內(nèi)容
        

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

       [ 2017年7月5日 ] 【

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

      一、直接使用MSI安裝包

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

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

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

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

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

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

      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)的在安裝時不共享的那些程序。

       如上圖,在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ù):

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

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

      那么,我們要做的很顯然是搜索這些鍵,查找哪一個才是我們要卸載的產(chǎn)品,然后用UninstallString把它卸掉就行了。另外我們需要在參數(shù)上加上/quiet和/qn參數(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
      首頁 1 2 尾頁
      將考試網(wǎng)添加到收藏夾 | 每次上網(wǎng)自動訪問考試網(wǎng) | 復(fù)制本頁地址,傳給QQ/MSN上的好友 | 申請鏈接 | 意見留言 TOP
      關(guān)于本站  網(wǎng)站聲明  廣告服務(wù)  聯(lián)系方式  站內(nèi)導(dǎo)航  考試論壇
      Copyright © 2007-2013 中華考試網(wǎng)(Examw.com) All Rights Reserved