windows - 从程序和功能中自动卸载

标签 windows python-2.7 uninstallation pywinauto addremoveprograms

我尝试使用 Python 自动化从 Windows 7 和 Windows 8.1 中卸载几个应用程序。 Windows 命令行也可以使用。

这些程序出现在控制面板的程序和功能列表中。单击它们并选择卸载将毫无问题地卸载它们。通过单击“程序和功能”菜单来手动卸载可以正常且轻松地进行。

这些程序是使用 EXE 文件而不是 MSI 文件安装的。

到目前为止我已经尝试过:

1)

wmic product get name

使用命令“wmic 产品获取名称”仅显示“程序和功能”页面上显示的部分程序的列表。我要卸载的程序未列出。

2)

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Uninstall

这些程序没有出现在上述注册表位置

3)

“使用 pywinauto 直接打开和操作“程序和功能”窗口。”

pywinauto 模块(或任何其他可以查找和操作窗口和按钮句柄的模块)确实可以打开并抓取“程序和功能”窗口,但操作失败。特别是,在搜索框中输入文本失败,因此无法选择要卸载的程序。

4)

“使用程序附带的卸载 msi。”

没有。

5)

“再次运行安装程序可执行文件。”

这只是更新软件,而不是删除它。

最佳答案

我写了一个uninstall example for 7-Zip使用 pywinauto 0.5.2。它在 Windows 7 和 Windows 8.1 上都能稳定运行。我相信它对其他人有用。

当然,这只是一个演示示例,因为可以通过“wmic”命令和相应参数简单地卸载 7-Zip。

from __future__ import print_function
import pywinauto

pywinauto.Application(backend="win32").start(r'explorer.exe')
explorer = pywinauto.Application(backend="win32").connect(path='explorer.exe')

# Go to "Control Panel -> Programs and Features"
NewWindow = explorer.window(top_level_only=True, active_only=True, class_name='CabinetWClass')
try:
    NewWindow.AddressBandRoot.click_input()
    NewWindow.type_keys(r'Control Panel\Programs\Programs and Features{ENTER}', with_spaces=True, set_foreground=False)
    ProgramsAndFeatures = explorer.window(top_level_only=True, active_only=True, title='Programs and Features', class_name='CabinetWClass')

    # Wait while list of programs is loading
    explorer.wait_cpu_usage_lower(threshold=5)

    item_7z = ProgramsAndFeatures.FolderView.get_item('7-Zip 9.20 (x64 edition)')
    item_7z.ensure_visible()
    item_7z.click_input(button='right', where='icon')
    explorer.PopupMenu.menu_item('Uninstall').click()

    Confirmation = explorer.window(title='Programs and Features', class_name='#32770', active_only=True)
    if Confirmation.exists():
        Confirmation.Yes.click_input()
        Confirmation.wait_not('visible')

    WindowsInstaller = explorer.window(title='Windows Installer', class_name='#32770', active_only=True)
    if WindowsInstaller.exists():
        WindowsInstaller.wait_not('visible', timeout=20)

    SevenZipInstaller = explorer.window(title='7-Zip 9.20 (x64 edition)', class_name='#32770', active_only=True)
    if SevenZipInstaller.exists():
        SevenZipInstaller.wait_not('visible', timeout=20)

    if '7-Zip 9.20 (x64 edition)' not in ProgramsAndFeatures.FolderView.texts():
        print('OK')
finally:
    NewWindow.close()

关于windows - 从程序和功能中自动卸载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31810538/

相关文章:

perl - 在 Linux 上,如何卸载从源代码构建的 Perl 版本?

batch-file - 将自定义 CLI 参数传递给 Inno Setup 卸载程序

windows - Win64 异常堆栈遍历不显示条目

python - 当 `pop` 循环列表时 `for` -ing 元素发生了什么

Python:从二进制数中去除字符串引号

python - 类型提示 lambda 函数作为函数参数

windows - WinRM 无法处理该请求。错误 0x80090311

windows - 如何在不传递文件的情况下排除 XCOPY 中的文件类型?

windows - 在 Windows 中使用 IOCP 时缓存 OVERLAPPED 结构

makefile - 'make install' 的反义词是什么,即如何在 Linux 中卸载库?