65.9K
CodeProject 正在变化。 阅读更多。
Home

MSIUninstaller.exe (控制台应用程序)

starIconstarIconstarIconemptyStarIconemptyStarIcon

3.00/5 (1投票)

2008年8月7日

CPOL
viewsIcon

23389

downloadIcon

229

这个工具可以轻松卸载所有先前安装的 MSI 包。

MSIUninstaller_bin

引言

我经常需要将网络中的许多机器回滚到我们软件的旧版本(以 MSI 包的形式分发)。
因此,我制作了一个简单的控制台应用程序,可以从 BAT 脚本或其他类似脚本运行。它允许我卸载计算机上的 MSI 包,即使我不知道当前安装了哪个版本。此外,如果已经安装了正确的(旧版本),我可以跳过该包的卸载。

Using the Code

MSIUninstaller.exe 调用 Microsoft 的 MsiExec.exe 来处理 MSI 包的实际卸载。
它在注册表数据库下查找所有已安装的包:
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall.
然后它循环遍历它们,找到与特定输入参数匹配的包,然后卸载它们。
该程序允许您指定输入参数,例如静默模式、日志和通配符。

此函数展示了程序的主要功能

    Sub Process(ByVal DisplayName As String, ByVal bUninstall As Boolean, _
	ByVal bDisplay As Boolean, ByVal bQuiet As Boolean, ByVal bLog As Boolean, _
	ByVal strSkip As String)

        ' Retrieve all the subkeys for the specified key.
        Dim names As String() = m_rKey.GetSubKeyNames
        Dim version As String = ""
        Dim publisher As String = ""
        Dim dispName As String = ""
        Dim iFound As Integer

        For Each s As String In names
            dispName = GetKeyValue(s, "DisplayName")
            If dispName <> "" Then
                If WildCardCompare(dispName, DisplayName) Then
                    version = GetKeyValue(s, "DisplayVersion")
                    publisher = GetKeyValue(s, "Publisher")
                    If strSkip <> "none" And WildCardCompare(version, strSkip) Then
                        Continue For
                    End If
                    Dim cmd As String = Environment.SystemDirectory.ToString + _
					"\MsiExec.exe /x " + s
                    If bQuiet Then
                        cmd += " /qn"
                    End If
                    If bLog Then
                        cmd += " /lie+c:\MSIUninstaller.log.txt"
                    End If
                    If Not bDisplay Then
                        Console.WriteLine("Publisher       = " + publisher)
                        Console.WriteLine("DisplayName     = " + dispName)
                        Console.WriteLine("version         = " + version)
                        Console.WriteLine("GUID            = " + s)
                        Console.WriteLine("EsitmatedSize   = " + (CDbl(GetKeyValue_
			(s, "EstimatedSize")) / 1024).ToString("N2") + " Mb")
                        Dim dato As String = GetKeyValue(s, "InstallDate")
                        dato = dato.Substring(6) + "-" + dato.Substring(4, 2) + _
						"-" + dato.Substring(0, 4)
                        Console.WriteLine("InstallDate     = " + CDate(dato).ToString_
							("dd-MMM-yyy"))
                        Console.WriteLine("InstallSource   = " + GetKeyValue_
						(s, "InstallSource") + vbLf)
                    End If
                    If bUninstall Then
                        Shell(cmd)
                    End If
                    iFound += 1
                End If
            End If
        Next s
        If Not bQuiet And iFound = 0 Then
            Console.WriteLine(DisplayName + " was not found in HKEY_LOCAL_MACHINE\_
		Software\Microsoft\Windows\CurrentVersion\Uninstall _
		on this computer" + vbLf)
        End If
    End Sub

    Private Function GetKeyValue(ByVal keyName As String, ByVal valueName As String) _
	As String
        Dim obj As Object = m_rKey.OpenSubKey(keyName).GetValue(valueName)
        If Not obj Is Nothing Then
            Return obj
        Else
            Return ""
        End If
    End Function

    Private Function WildCardCompare(ByVal strInput As String, _
	ByVal strWildCard As String) As Boolean
        strWildCard = "^" + Regex.Escape_
	(strWildCard).Replace("\*", ".*").Replace("\?", ".") + "$"
        Return Regex.IsMatch(strInput, strWildCard, RegexOptions.IgnoreCase)
    End Function	

历史

  • 2008 年 8 月 7 日:初始发布
© . All rights reserved.