Vb. 2 ways to invoke MSI uninstall software in net _vb.net

Source: Internet
Author: User
Tags trim

Recently in the toss group inside the broken lab, to write their own program every day to install the latest version of the build. Today, there is no task at hand, so write down some of the things you use for future reference. This log to record how the. NET to uninstall the other software.

First, use the MSI installation package directly

If you know the path to the MSI installer, you can obviously use it directly:

Copy Code code as follows:

msiexec/x "c:table Manager clients.msi"/quiet/qn

The/quiet parameter represents an automatic uninstall, and/qn indicates that any UI is displayed.

This method is very simple and recommended for use. But if the software version is wrong, or if the installer does have a problem (such as a wonderful installation program for us), then it's not going to work.

Copy Code code as follows:

Msiexec/option <required parameter> [Optional Parameter]

Install Options
</package | /i> <Product.msi>
Installs or configures a product

However, the serial number is variable and the serial number is not necessarily the same for different versions of the same program (a new serial number may be generated). In order to get the serial number of the product required, you can only check the registration form.

Second, the use of product serial number Uninstall program

All installed with MSI program will be recorded in the HKEY_LOCAL_MACHINE Softwaremicrosoftwindowscurrentversioninstalleruserdatas-1-5-18products Zijian. S-1-5-18 is a universal user, there may be other user directories (such as I have s-1-5-21-2109753547-707883502-1543859470-98763), should be corresponding to the installation of those who do not share those programs.

As shown above, there is a whole bunch of hexadecimal digits under the products key. There may be installproperties in the number (note that not every one has it), and then there are displayname to identify the product name, DisplayVersion to display the version number, and so on. We just need to focus on what we can use, just focus on the product name.

The number displayed on the left side is not the product serial number for MSI uninstall. We notice that there is a uninstallstring attribute, which is the command and parameter to uninstall the program:

Copy Code code as follows:

MSIEXEC.EXE/X{4B9E6EB0-0EED-4E74-9479-F982C3254F71}

So what we're going to do is obviously search these keys, find out which one is the product we want to uninstall, and then use UninstallString to unload it. In addition, we need to add/quiet and/qn parameters to the parameters, so that we can implement automatic uninstall.

Copy Code code as follows:

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. Don't need to uninstall. "

Logdataaccess.insertapplicationlog (Tmconfigrationmanager.getconfig ("ServerType"), message)
Console.WriteLine (Message)
End Sub

. NET, the classes that access the registry are encapsulated under the Microsoft.Win32 namespace and can be used directly (mainly using the RegistryKey class, regisitrykey similar to the tree structure).

This is the code that implements the automatic uninstall (there are some code associated with the output log that can be used without it).

The program first search all products under the product key, if there are installproperties subkeys, match displayname whether and to uninstall the same program, if the same, generate an uninstall command and start a new process to uninstall.

If the uninstall fails, MSIEXEC returns a value of not 0, at which point we output the error message. (Note: There are two more values indicating that the uninstall was successful but need to be restarted, please find the relevant manual yourself.) )

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.