从程序和功能自动卸载

时间:2015-08-04 13:22:56

标签: windows python-2.7 uninstall pywinauto addremoveprograms

我正在尝试使用Python自动化从Windows 7和Windows 8.1卸载一些应用程序。 Windows命令行也可以使用。

程序显示在控制面板的“程序和功能”列表中。单击它们并选择卸载将毫无问题地卸载它们。通过单击“程序和功能”菜单手动卸载可以很好地轻松完成。

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

到目前为止我尝试过:

1)

wmic product get name

使用命令' wmic product get name'显示“程序和功能”中仅显示的一些程序的列表。页。我没有列出我想要卸载的程序。

2)

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Uninstall

程序未出现在上述注册表位置

3)

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

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

4)

"使用程序附带的卸载msi。"

没有一个。

5)

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

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

1 个答案:

答案 0 :(得分:2)

我使用pywinauto 0.5.2写了uninstall example for 7-Zip。它在Windows 7和Windows 8.1上都适用于我。我相信它对其他人有用。

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

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()