如何实现Windows应用在开机/休眠恢复时为所有用户自动启动?
Got it, let's work through this problem together. You need your application to launch automatically for every user on two occasions: when the system starts up after a shutdown, and when waking from sleep or hibernation. Your previous attempts with shell:startup and the HKLM Run registry key fell short because they didn't cover all users or the sleep/wake scenario—here's how to fix it:
1. Ensure All-User Startup (Post-Shutdown)
The HKLM Run registry key does work for all users, but you might have missed a critical detail in the command syntax. Let's get this right:
- Open Command Prompt as Administrator
- Run this command, replacing
YourAppNamewith a unique identifier for your app andC:\Full\Path\To\YourApp.exewith the absolute path to your executable (wrap the path in quotes if it contains spaces):REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" /v "YourAppName" /t REG_SZ /d "\"C:\Full\Path\To\YourApp.exe\"" /f - If you're running a 32-bit app on a 64-bit system, use this 32-bit registry path instead:
REG ADD "HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Run" /v "YourAppName" /t REG_SZ /d "\"C:\Full\Path\To\YourApp.exe\"" /f
This ensures your app launches for every user when the system boots up. The nested quotes are essential—without them, the registry entry will fail if your path has spaces.
2. Trigger App on Sleep/Hibernation Wake
Registry Run keys only fire on system startup, not when waking from sleep. To cover this scenario, Task Scheduler is the most reliable tool—it lets you trigger actions on system wake and can be configured for all users:
Step-by-Step Task Setup
- Open Task Scheduler by searching for
taskschd.mscin the Start Menu (run it as Administrator for full access). - Click Create Task (skip "Create Basic Task"—we need advanced options).
- General Tab:
- Name your task (e.g., "Launch My App on Wake")
- Check Run whether user is logged on or not
- Check Run with highest privileges (avoids UAC blocks that might prevent the app from launching)
- Set the "Configure for" dropdown to your Windows version (e.g., Windows 10/11)
- Triggers Tab:
- Click New
- Under "Begin the task", select Workstation unlock (this triggers when any user wakes the system and unlocks it)
- In the "Settings" section, select Any user to ensure this works for all accounts
- Click OK to save the trigger
- (Optional) Add a "At startup" trigger here too if you want to combine both startup and wake triggers in one task, instead of using the registry
- Actions Tab:
- Click New
- Under "Action", select Start a program
- In "Program/script", browse to your app's executable or paste the full absolute path (use quotes if there are spaces)
- Click OK
- Conditions Tab:
- Uncheck Start the task only if the computer is on AC power if you want the app to launch even when on battery
- Leave other options as default
- Settings Tab:
- Check If the task fails, restart every [X] minutes (e.g., 5 minutes) to ensure the app launches successfully if there's a temporary issue
- Check If the task is already running, then do not start a new instance to prevent duplicate app windows
- Click OK to save the task
Key Notes to Avoid Issues
- Store your app in a location accessible to all users (e.g.,
C:\Program Files\YourApp), not in a user-specific folder likeDocumentsorDesktop. - Test both scenarios: shut down the system, boot up, log into different users to confirm the app runs; then put the system to sleep/hibernation, wake it, unlock, and verify the app launches.
内容的提问来源于stack exchange,提问作者Rocky

