如何列出计算机上安装的所有程序并卸载它们
列出并卸载客户端计算机上安装的所有程序。
介绍
本文将教您如何列出计算机上安装的所有软件及其信息。本文还将解释如何卸载软件。
要求
特点
- 命名空间 "Microsoft.Win32"
- 注册表知识。
- 命名空间 "System.Diagnostics"
希望您喜欢这篇文章!!!!
- 列出客户端计算机上安装的所有软件。
- 列出所有软件信息。
- 卸载软件。
理解和使用注册表
注册表项是完成此工作的关键。
需要理解的术语
- 当我说到旧版时,我指的是一些在线资源提供的旧注册表项。
- 提到新版时,指的是本文和程序卸载程序中使用的注册表项。
注册表项在哪里以及它与其他开发者的不同之处。
在我创建这个应用程序之前,我根本不清楚它们在哪里以及它们是如何工作的。我在网上搜索并找到了一些资源。这些资源帮助我列出了一些已安装的软件,但不是全部。我通过比较 Vista 中的“程序和功能”(XP 中的“添加或删除程序”)来弄清楚这一点。差异很大。我的旧列表与“程序和功能”中列出的程序数量相差甚远。所以我决定寻找新的注册表项。我发现了两个项,一个项只有五个软件,另一个项几乎和“程序和功能”中发现的软件数量一样多。我现在将告诉您在线资源的注册表项和本文中使用的注册表项。
本文使用的注册表项会从注册表中返回类似如下的图像。
- 在线资源注册表项
- 本文使用的注册表项:“SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products”。这是从 LocalMachine 获取的。
![]()
注册表项中的软件信息。
下图显示了所有包含软件信息的项。打开其中一个项后,会找到四个不同的项,其中包含软件信息。
我们将使用“InstallProperties”项,因为它包含用户可以理解的信息。InstallProperties 项将返回类似如下的图像。
从下图开始,我将向您解释哪些项及其数据含义。我只会解释重要的项。
联系方式
:
寻求支持的联系人。显示名称
:
软件的名称。显示版本
:
软件的版本。估算大小:
软件的大小。 (您需要将其转换为整数,以便用户理解)帮助链接
:
访问帮助的链接。帮助电话:
帮助电话号码。安装日期:
该软件的安装日期。安装位置:
安装位置。安装源:
安装目录。语言:
该软件支持的语言。本地包:
MSI 文件位置。ModifyPath:
此处返回的字符串大致如下:MsiExec.exe /I{AC76BA86-7AD7-1033-7B44-A81200000003}
我将在本文的某个部分更详细地解释这一点。发布者:
软件的发布者。自述文件:
此软件的自述文件位置。UninstallString:
uninstallString 返回的值与 ModifyPath 类似。 稍后我会详细解释。URLInfoAbout:
访问以获取产品更多信息的 URL。URLUpdateInfo
:
访问以获取更新信息的 URL。版本:
软件的版本。
产品卸载: MsiExec.exe /I{AC76BA86-7AD7-1033-7B44-A81200000003} 到底是什么?
当我第一次看到这个字符串时,我也是这么想的。 我对此进行了一些研究。 真正帮助我的是 Microsoft TechNet 网站。 http://technet.microsoft.com/en-us/library/bb490936.aspx.
以下内容将教您关于语法和其他可用的参数。
语法
msiexec /f [p][o][e][d][c][a][u][m][s][v]{package|ProductCode}
参数
/f : 启用下表中列出的一项或多项命令行选项。
命令
描述
p
仅当文件丢失时才重新安装。
o
如果文件丢失或安装了旧版本,则重新安装。
e
如果文件丢失或安装了相等或更旧的版本,则重新安装。
d
如果文件丢失或安装了不同版本,则重新安装。
c
如果文件丢失或存储的校验和与计算值不匹配,则重新安装。
a
强制重新安装所有文件。
u
重写所有必需的用户特定注册表项。
m
重写所有必需的计算机特定注册表项。
s
覆盖所有现有快捷方式。
v
从源运行并重新缓存本地包。
package : Windows Installer 包文件的名称。
ProductCode : Windows Installer 包的全局唯一标识符 (GUID)。
您可以使用 msiexec 和上面显示的任何命令来执行操作。
卸载产品。
要声明的变量
第一部分:MsiExec.exe
String uninstallString
- 将保存要卸载产品卸载字符串的字符串。Process UninstallProcess
- 将运行卸载的进程。String temp
- 保存卸载字符串的第 2 部分。
第二部分:/I{AC76BA86-7AD7-1033-7B44-A81200000003}
“UninstallProcess”……它是如何工作的……
分割后文件名变为 MsiExec.exe。 这将运行 Microsoft Installer。
UninstallProcess.StartInfo.FileName = _ uninstallstring.Split("/".ToCharArray)(0)
参数 temp 将告知卸载程序要卸载哪个产品,因为 temp 保存了 ProductCode。
UninstallProcess.StartInfo.Arguments = temp
运行卸载程序UninstallProcess.Start()
'UninstallProductname should hold the UninstallString from the Registry Dim uninstallString as string Dim UninstallProcess As New System.Diagnostics.Process() 'temp will hold something like this: /I{AC76BA86-7AD7-1033-7B44-A81200000003} Dim temp As String = uninstallstring.Substring(12) 'replacing with /x with /i would cause another popup of the application uninstall 'The FileName will be: MsiExec.exe UninstallProcess.StartInfo.FileName = uninstallstring.Split("/".ToCharArray)(0) UninstallProcess.StartInfo.Arguments = temp UninstallProcess.Start()
列出所有已安装的软件……
' HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products Dim Software As String = Nothing 'The registry key will be held in a string SoftwareKey. Dim SoftwareKey As String = "SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products" Using rk As RegistryKey = Registry.LocalMachine.OpenSubKey(SoftwareKey) For Each skName In rk.GetSubKeyNames 'Get sub keys Dim name = Registry.LocalMachine.OpenSubKey(SoftwareKey).OpenSubKey(skName).OpenSubKey("InstallProperties").GetValue("DisplayName") 'Registry.LocalMachine.OpenSubKey(SoftwareKey).OpenSubKey(skName).OpenSubKey("InstallProperties").GetValue("DisplayName") Dim installocation = Registry.LocalMachine.OpenSubKey(SoftwareKey).OpenSubKey(skName).OpenSubKey("InstallProperties").GetValue("InstallLocation") 'InstallProperties Dim publisher = Registry.LocalMachine.OpenSubKey(SoftwareKey).OpenSubKey(skName).OpenSubKey("InstallProperties").GetValue("Publisher") Dim uninstallString = Registry.LocalMachine.OpenSubKey(SoftwareKey).OpenSubKey(skName).OpenSubKey("InstallProperties").GetValue("UninstallString") 'Add the Software information to lstPrograms If name.ToString <> "" Then 'Declare new ListView Item Dim list As New ListViewItem 'Set Text Property to Name of Software list.Text = name.ToString 'Add Install Location list.SubItems.Add(installocation.ToString) 'Add Publisher list.SubItems.Add(publisher.ToString) ' Add it to lstPrograms, a listview that will hold our Software List. lstPrograms.Items.Add(list) End If 'next Next End Using
所有源代码
卸载软件
'UninstallProductname should hold the UninstallString from the Registry Dim uninstallString as string Dim UninstallProcess As New System.Diagnostics.Process() 'temp will hold something like this: /I{AC76BA86-7AD7-1033-7B44-A81200000003} Dim temp As String = uninstallstring.Substring(12) 'replacing with /x with /i would cause another popup of the application uninstall 'The FileName will be: MsiExec.exe UninstallProcess.StartInfo.FileName = uninstallstring.Split("/".ToCharArray)(0) UninstallProcess.StartInfo.Arguments = temp UninstallProcess.Start()将所有软件列出到 ListView (lstPrograms) 中
' HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products Dim Software As String = Nothing 'The registry key will be held in a string SoftwareKey. Dim SoftwareKey As String = "SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products" Using rk As RegistryKey = Registry.LocalMachine.OpenSubKey(SoftwareKey) For Each skName In rk.GetSubKeyNames 'Get sub keys Dim name = Registry.LocalMachine.OpenSubKey(SoftwareKey).OpenSubKey(skName).OpenSubKey("InstallProperties").GetValue("DisplayName") 'Registry.LocalMachine.OpenSubKey(SoftwareKey).OpenSubKey(skName).OpenSubKey("InstallProperties").GetValue("DisplayName") Dim installocation = Registry.LocalMachine.OpenSubKey(SoftwareKey).OpenSubKey(skName).OpenSubKey("InstallProperties").GetValue("InstallLocation") 'InstallProperties Dim publisher = Registry.LocalMachine.OpenSubKey(SoftwareKey).OpenSubKey(skName).OpenSubKey("InstallProperties").GetValue("Publisher") Dim uninstallString = Registry.LocalMachine.OpenSubKey(SoftwareKey).OpenSubKey(skName).OpenSubKey("InstallProperties").GetValue("UninstallString") 'Add the Software information to lstPrograms If name.ToString <> "" Then 'Declare new ListView Item Dim list As New ListViewItem 'Set Text Property to Name of Software list.Text = name.ToString 'Add Install Location list.SubItems.Add(installocation.ToString) 'Add Publisher list.SubItems.Add(publisher.ToString) ' Add it to lstPrograms, a listview that will hold our Software List. lstPrograms.Items.Add(list) End If 'next Next End Using
结论
这个应用程序是开发人员与 Windows API 和注册表交互的绝佳方式。 您还可以学习 Windows 环境中一些调用的实现方式。 我真的希望看到开发人员在他们的应用程序中使用本文中展示的技术。