“适当的” &在Python中获取所有已安装的Windows程序的可靠方法?

发布于 2024-08-18 10:30:31 字数 745 浏览 9 评论 0原文

我见过很多用 python 检索 WinXP+ 上安装的程序的方法。执行此操作的正确最可靠方法是什么?

目前,我正在访问 HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall 并从那里读取每个键以获取列表。 (有人告诉我这不是正确的做事方式)我也看到过使用 WMI/Win32com 来执行此操作的示例,但也看到过评论以及 WMI 可能的实现在某些机器上被关闭,这不是一个非常可靠的解决方案。

有没有一种既正确又可靠的方法来获取已安装程序的列表?我见过的 WMI 示例都不能在这台机器上运行(因此我不愿意使用它,我只运行 WinFLP;这是 XP 的精简版本。)

我似乎还找到了我搜索的 TechNet 文章已经出现,它为我的问题提供了类似的答案: http://gallery.technet.microsoft.com/ScriptCenter/en-us/154dcae0-57a1-4c6e-8f9f-b215904485b7 请注意,平台下列出的 Vista/7 非常明确地表示“不”。 .行不通。因此,WMI 交易似乎是行不通的...

能够检索安装的路径也将是一个好处,因为现在我当前的代码不考虑安装在另一个驱动器上或非默认驱动器上的人目录。

I've seen numerous ways of retrieving installed programs on WinXP+ in python. What is the proper and most robust way of doing this?

Currently I'm accessing HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall and reading each of the keys from there to get a list. (I've been told this isn't the proper way of doing things) I've seen examples of using WMI/Win32com to do this as well but have seen comments along with those implementations that WMI might be turned off on certain machines and that it's not a very reliable solution.

Is there a method which is both proper, and reliable to get a list of installed programs? None of the WMI examples I've seen have worked on this machine (hence my reluctance to use it, I'm only running WinFLP; which is a stripped vers of XP.)

I seem to have also found the TechNet article which my searches have turned up which is provided to a similar answer on my question: http://gallery.technet.microsoft.com/ScriptCenter/en-us/154dcae0-57a1-4c6e-8f9f-b215904485b7 Note that Vista/7 listed under Platforms very clearly says "Nope"...won't work. So the WMI deal seems like it's a no-go...

Being able to retrieve the installed path would be an upside as well, as right now my current code does not account for someone installing on another drive, or in a non-default directory.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟花易冷人易散 2024-08-25 10:30:31

您提到的 technet 脚本在 Win 7(使用 Python 2.5 32 位)下完美运行,我真的不明白为什么它不应该。

实际上,WMI 方法的真正弱点在于它仅列出通过 Windows Installer 安装的产品。所以它不会给你完整的列表。许多程序使用不同的安装程序。只需比较 (Select * from Win32_Product) 和控制面板中显示的结果即可。因此,除非您确定列表中插入的程序是通过 MSI 安装的,否则 WMI 绝对不是答案。

所以它可能不是很Pythonic,但据我所知,最好的方法是像您所做的那样使用注册表。这实际上就是控制面板的工作方式,因此至少 Windows 认为这是最可靠的方式。

The technet script you refer to perfectly works under Win 7 (with Python 2.5 32bits), and I really cannot see why it shouldn't.

Actually, the real weakness of the WMI approach is that it only lists products installed through the Windows Installer. So it's will not give you the full list. Many programs use different installers. Just compare the results between the (Select * from Win32_Product) and what is displayed in the Control Panel. So, unless you are sure that the program that interset you in your listing are installed with MSI, WMI is definitely not an answer.

So it may be not very pythonic, but the best way, as far as I know, is to use the registry as you've done. This is actually how the control panel works, so at least Windows considers it to be the most robust way to do it.

慵挽 2024-08-25 10:30:31

WMI 是查找已安装程序的正确方法,因为它可以跨不同版本的操作系统运行,并且今后将得到支持。查找特定的注册表项可能适用于特定版本的 Windows,但不保证将来也能正常工作。下面是一些简单的 Python 代码,用于检查 Box Sync,我刚刚在 Windows 7 上尝试过。请注意,并非所有字段都适用于每个产品,因此请注意,这些字段将为“无”。

import wmi
w = wmi.WMI()
for p in w.Win32_Product():
    if 'Box, Inc.' == p.Vendor and p.Caption and 'Box Sync' in p.Caption:
        print 'Installed {}'.format(p.Version)

我发现 WMI 的缺点是启动速度非常慢。

WMI is the correct way to look for installed programs as it will work across different versions of the OS and will be supported going forward. Looking for specific regkeys may work fine for specific versions of Windows but is not guaranteed to work in the future. Here is some simple python code to check for Box Sync which I just tried on Windows 7. Note that not all fields will be available for every product so be aware these will be 'None.'

import wmi
w = wmi.WMI()
for p in w.Win32_Product():
    if 'Box, Inc.' == p.Vendor and p.Caption and 'Box Sync' in p.Caption:
        print 'Installed {}'.format(p.Version)

The downside I have seen with WMI is it is very slow to start up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文