如何通过Windows注册表实现网页调用Notepad.exe仅打开指定name.txt文件
Absolutely, you can make this work! Let's break this into two core parts: configuring Windows to force Notepad to open your specific name.txt every time it launches, and setting up a web button to trigger this action reliably.
First, let's handle the Registry setup—this covers both web-triggered launches and making Notepad always open name.txt when started normally.
For Web Button Trigger (Custom Protocol)
This creates a unique URL protocol that tells Windows to launch Notepad with your file when called:
- Open Registry Editor (
regedit.exe) as Administrator (you need elevated rights to modifyHKEY_CLASSES_ROOT). - Navigate to
HKEY_CLASSES_ROOT, right-click, and create a new key named something likemy-notepad-launcher(pick a unique name to avoid conflicts with existing protocols). - Inside this new key:
- Set the Default string value to
"URL:Notepad Launcher for name.txt"(this labels the protocol for Windows). - Create a new string value named
URL Protocoland leave its data blank (this marks it as a valid protocol handler).
- Set the Default string value to
- Now build the subkey structure: right-click
my-notepad-launcher> New > Key, name itshell. Then insideshell, create another key namedopen, and insideopen, create a key namedcommand. - In the
commandkey, set the Default value to the full path of Notepad plus your target file. For example:
Replace"C:\Windows\System32\notepad.exe" "C:\your\file\path\name.txt"C:\your\file\path\name.txtwith the actual absolute path to your file.
For All Notepad Launches (Always Open name.txt)
If you want Notepad to open name.txt every time anyone launches it (double-clicking the exe, start menu, etc.), modify its default command:
- Navigate to
HKEY_CLASSES_ROOT\Applications\notepad.exe\shell\open\command. - Double-click the Default value. If it currently has
"%1"(which lets Notepad open the clicked file or a blank window), replace it with your file path. For example:
Original:
Modified:"C:\Windows\System32\notepad.exe" "%1"
Note: This overrides default behavior—if you ever want to go back, restore the"C:\Windows\System32\notepad.exe" "C:\your\file\path\name.txt""%1"parameter.
Now that the Registry is configured, you can add a button to your web page that triggers the custom protocol. Here are two simple options:
Option 1: Simple Anchor Tag
<a href="my-notepad-launcher://" target="_blank">Open name.txt in Notepad</a>
Option 2: Button with JavaScript
<button onclick="window.location.href='my-notepad-launcher://'">Open name.txt in Notepad</button>
Key Notes for Web Deployment
- Most browsers will show a security prompt asking the user to allow the action—this is normal, and users need to click "Allow" to proceed.
- Make sure the protocol name (
my-notepad-launcher) matches exactly what you created in the Registry. - If deploying to multiple users, ensure
name.txtexists in the same absolute path for everyone, or use a network share path (like\\server\shared\name.txt) that all users can access.
内容的提问来源于stack exchange,提问作者User90

