windows 通过注册表检测已安装的程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/429738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:55:51  来源:igfitidea点击:

Detecting installed programs via registry

windowsregistry

提问by Mark Stahler

I need to develop a process that will detect if the users computer has certain programs installed and if so, what version. I believe I will need a list with the registry location and keys to look for and feed it to the program which is not a problem. Is there a better way to accomplish this?

我需要开发一个过程来检测用户计算机是否安装了某些程序,如果安装了,是什么版本。我相信我需要一个包含注册表位置和键的列表来查找并将其提供给程序,这不是问题。有没有更好的方法来实现这一点?

My first thought was to check in the registry in the uninstallation entries but it seems one of the apps I wish to detect does not have one. What is the standard location for all registry using applications to make an entry in?

我的第一个想法是在卸载条目中检查注册表,但我希望检测的应用程序之一似乎没有。使用应用程序进行输入的所有注册表的标准位置是什么?

采纳答案by Piskvor left the building

User-specific settings should be written to HKCU\Software, machine-specific settings to HKLM\Software. Under these keys, structure [software vendor name]\[application name](e.g. HKLM\Software\Microsoft\Internet Explorer) may be the most common, but that's just a convention, not a law of nature.

特定于用户的设置应写入 HKCU\Software,特定于机器的设置应写入 HKLM\Software。在这些键下,结构[software vendor name]\[application name](例如HKLM\Software\Microsoft\Internet Explorer)可能是最常见的,但这只是惯例,而不是自然法则。

Many (most?) applications also add their uninstall entries to HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall\[app name], but again, not all applications do this.

许多(大多数?)应用程序还将它们的卸载条目添加到 中HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall\[app name],但同样,并非所有应用程序都这样做。

These are the most important keys; however, contents of the registry do not have to represent the installed software exactly - maybe the application was installed once, but then was manually deleted, or maybe the uninstaller didn't remove all traces of it. If you want to be sure, check the filesystem to see if the application still exists where its registry entries say it is.

这些是最重要的键;然而,注册表的内容不必完全代表已安装的软件——也许应用程序安装了一次,但随后被手动删除,或者卸载程序没有删除它的所有痕迹。如果您想确定,请检查文件系统以查看该应用程序是否仍然存在于其注册表项所显示的位置。

Edit:

编辑:

If you're a member of the group Administrators, you can check the HKEY_USERShive - each user's HKCU actually resides there (you'll need to know the user SID, or go through all of them).

如果您是管理员组的成员,您可以检查HKEY_USERS配置单元 - 每个用户的 HKCU 实际上都驻留在那里(您需要知道用户 SID,或查看所有用户名)。

Note: As @Brian Ensink says, "installed" is a bit of a vague concept - are we trying to find what the user could run? Some software doesn't even write to the Registry at all: search for "portable apps" to see apps that have been specifically modified to run directly from media (CD/USB) and notto leave any traces on the computer. We may also have to scan the disks, and network disks, and anything the user downloads, and world-accessible Windows shares in the Internet (yes, such things exist legitimately - \\live.sysinternals.com\toolscomes to mind). In this direction, there's no real limit of what the user can run, unless prevented by system policies.

注意:正如@Brian Ensink 所说,“已安装”是一个有点模糊的概念——我们是否试图找到用户可以运行的内容?有些软件甚至根本不写入注册表:搜索“便携式应用程序”以查看经过专门修改以直接从媒体 (CD/USB) 运行且不在计算机上留下任何痕迹的应用程序。我们可能还需要扫描磁盘、网络磁盘、用户下载的任何内容,以及 Internet 上全球可访问的 Windows 共享(是的,这些东西合法存在 -\\live.sysinternals.com\tools想到了)。在这个方向上,用户可以运行的内容没有真正的限制,除非被系统策略阻止。

回答by Bernd Ott

On 64-bit systems the x64 key is:

在 64 位系统上,x64 密钥是:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall

Most programs are listed there. Look at the keys: DisplayNameDisplayVersion

大多数程序都列在那里。看钥匙: DisplayNameDisplayVersion

Note that the last is not always set!

请注意,最后一个并不总是设置!

On 64-bit systems the x86 key (usually with more entries) is:

在 64 位系统上,x86 密钥(通常有更多条目)是:

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall

回答by Brian Ensink

You could use MSI API to enumerate everything installed by Windows Installer but that won't list all the software available on a machine. Without knowing more about what you need I think the concept of "installed" is a little vague. There are many ways to deploy software to a system ranging from big complicated installers to ZIP files and everything in between.

您可以使用 MSI API 枚举 Windows Installer 安装的所有内容,但这不会列出机器上可用的所有软件。在不了解您需要什么的情况下,我认为“已安装”的概念有点模糊。将软件部署到系统的方法有很多种,从大型复杂的安装程序到 ZIP 文件以及介于两者之间的所有内容。

回答by MSalters

An application does not need to have any registry entry. In fact, many applications do not need to be installed at all. U3 USB sticks are a good example; the programs on them just run from the file system.

应用程序不需要有任何注册表项。事实上,很多应用程序根本不需要安装。U3 U 盘就是一个很好的例子;它们上的程序只是从文件系统运行。

As noted, most good applications can be found via their uninstall registry key though. This is actually a pair of keys, per-user and per-machine (HKCU/HKLM - Piskvor mentioned only the HKLM one). It does not (always) give you the install directory, though.

如前所述,大多数优秀的应用程序都可以通过卸载注册表项找到。这实际上是一对密钥,每个用户和每个机器(HKCU/HKLM - Piskvor 只提到了 HKLM 一个)。但是,它并不(总是)为您提供安装目录。

If it's in HKCU, then you have to realise that HKEY_CURRENT_USERreally means "Current User". Other users have their own HKCU entries, and their own installed software. You can't find that. Reading everyHKEY_USERShive is a disaster on corporate networks with roaming profiles. You really don't want to fetch 1000 accounts from your remote [US|China|Europe] office.

如果它在 HKCU 中,那么您必须意识到它的HKEY_CURRENT_USER真正含义是“当前用户”。其他用户有他们自己的 HKCU 条目,以及他们自己安装的软件。你找不到那个。读取每个HKEY_USERS配置单元对于具有漫游配置文件的企业网络来说是一场灾难。您真的不想从远程 [美国| CN |欧洲] 办公室获取 1000 个帐户。

Even if an application is installed, and you know where, it may not have the same "version" notion you have. The best source is the "version" resource in the executables. That's indeed a plural, so you have to find all of them, extract version resources from all and in case of a conflict decid on something reasonable.

即使安装了一个应用程序,并且您知道在哪里,它也可能与您所拥有的“版本”概念不同。最好的来源是可执行文件中的“版本”资源。这确实是一个复数形式,因此您必须找到所有这些资源,从所有资源中提取版本资源,并在发生冲突时做出合理的决定。

So - good luck. There are dozes of ways to fail.

所以 - 祝你好运。失败的方法有很多种。

回答by Arivan Bastos

You can use a PowerShell script to look at registers and get the installed program details. The script bellow will generate a file with the complete list of installed programs. Save it with ".ps" extension and double click the file.

您可以使用 PowerShell 脚本查看寄存器并获取已安装程序的详细信息。下面的脚本将生成一个文件,其中包含已安装程序的完整列表。使用“.ps”扩展名保存并双击该文件。

#
# Generates a full list of installed programs.
#

# Temporary auxiliar file.
$tmpFile = "tmp.txt"

# File that will hold the programs list.
$fileName = "programas-instalados.txt"

# Columns separator.
$separator = ","

# Delete previous files.
Remove-Item $tmpFile
Remove-Item $fileName

# Creates the temporary file.
Create-Item $tmpFile

# Searchs register for programs - part 1
$loc = Get-ChildItem HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall
$names = $loc |foreach-object {Get-ItemProperty $_.PsPath}
foreach ($name in $names)
{
    IF(-Not [string]::IsNullOrEmpty($name.DisplayName)) {      
        $line = $name.DisplayName+$separator+$name.DisplayVersion+$separator+$name.InstallDate
        Write-Host $line
        Add-Content $tmpFile "$line`n"        
    }
}

# Searchs register for programs - part 2
$loc = Get-ChildItem HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall
$names = $loc |foreach-object {Get-ItemProperty $_.PsPath}
foreach ($name in $names)
{
    IF(-Not [string]::IsNullOrEmpty($name.DisplayName)) {      
        $line = $name.DisplayName+$separator+$name.DisplayVersion+$separator+$name.InstallDate
        Write-Host $line
        Add-Content $tmpFile "$line`n"
    }
}

# Sorts the result, removes duplicate lines and
# generates the final file.
gc $tmpFile | sort | get-unique > $filename

回答by David Airapetyan

In addition to all the registry keys mentioned above, you may also have to look at HKEY_CURRENT_USER\Software\Microsoft\Installer\Products for programs installed just for the current user.

除了上面提到的所有注册表项之外,您可能还需要查看 HKEY_CURRENT_USER\Software\Microsoft\Installer\Products 以获取仅为当前用户安装的程序。

回答by user10211111

HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Compatibility Assistant\Persisted

HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Compatibility Assistant\Persisted

回答by Naikrovek

Win32_Product never shows everything, only software installed via an MSI installer (as far as I can tell.)

Win32_Product 从不显示所有内容,只显示通过 MSI 安装程序安装的软件(据我所知。)

There are lots of software packages that get installed via other installers that don't show up in there. another way is needed.

有很多软件包是通过其他安装程序安装的,但没有出现在那里。需要另一种方式。

回答by Nick

Seems like looking for something specific to the installed program would work better, but HKCU\Software and HKLM\Software are the spots to look.

似乎寻找特定于已安装程序的东西会更好,但 HKCU\Software 和 HKLM\Software 是要查找的地方。