Chapter/Index: Introduction | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | Appendix
| Here provides practical code examples demonstrating how to open and close various applications and files in Windows using different programming approaches. It includes snippets that show how to open files with their default programs, close all running applications, close specific apps like Notepad or File Explorer, and even simulate key commands such as pressing F4 to close active windows. Open any type of files with default programs/apps: code: ================================================= Close all opened apps in windows, e.g. internet explorer, chrome, ...: code: ================================================= Close the app with F4 key: code: ================================================= Close the apps, which are in tab format, without killing the windows system only, e.g. not applicable to notepad, file explorer: code: ================================================= Close notepad app: code: ================================================= Close File Explorer app : code: The two Python scripts, (code A and code B), for closing File Explorer windows differ in both scope and robustness. The first version uses a PowerShell command to identify explorer.exe processes with visible window titles and then sends a polite CloseMainWindow() request to them. This method works safely without affecting the desktop or taskbar, but it only closes folder windows that are running in separate explorer.exe processes. If the user’s system runs all Explorer windows within the main shell process, this script will leave those windows open. The second version employs a more advanced approach using the Windows COM interface (Shell.Application). It iterates through all open shell windows and gracefully closes those corresponding to filesystem folders by calling the .Quit() method. This approach works reliably in both configurations—whether Explorer windows share the shell process or run independently—and preserves the desktop, taskbar, and non-filesystem windows like Control Panel.
|