下载演示项目- 6.26 KB
介绍
我有一个问题,我必须周期性地将网络上的许多机器回滚到我们的软件的较旧版本(以MSI包的形式分发)。因此,我做了一个简单的控制台应用程序,可以运行从BAT脚本或类似。它允许我卸载计算机上的MSI包,即使我不知道当前安装在计算机上的是哪个版本。而且,如果已经安装了正确的(旧版本),我可以跳过卸载包。
使用的代码
MSIUninstaller.exe调用microsoftmsiexec.exe来处理MSI包的实际卸载。它在HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall下的注册表数据库中查找所有已安装的包。然后循环遍历它们以找到与特定输入参数匹配的包,然后卸载它们。该程序允许您指定输入参数,如安静、日志和通配符。
这个函数显示了程序的主要功能:
隐藏,收缩,复制CodeSub 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日:初任
本文转载于:http://www.diyabc.com/frontweb/news10684.html