Files
osr-mono/packages/osr-code-bot/kbot-extensions/docs/shell-ext/windows.md
T
2025-02-01 14:25:34 +01:00

3.1 KiB

To register custom context menus (like "Open with") in Windows, you typically need to make changes to the Windows Registry. Below are some options and example code snippets you can use to achieve this. Keep in mind that editing the Windows Registry can have significant effects on your system, so proceed with caution and back up the registry before making any modifications.

Option 1: Manually Editing the Windows Registry

  1. Open the Registry Editor:

    • Press Win + R, type regedit, and hit Enter.
  2. Navigate to the file type you want to add the context menu for:

    • For example, for .txt files, navigate to HKEY_CLASSES_ROOT\txtfile\shell.
  3. Add a new key:

    • Right-click on shell, select New > Key, and name it as you like (e.g., OpenWithMyApp).
  4. Define the menu text:

    • Click on the newly created key, and in the right pane, double-click the (Default) value to set the menu name (e.g., "Open with MyApp").
  5. Specify the command to run:

    • Right-click on your new key (e.g., OpenWithMyApp), select New > Key, and name it command.
    • Set the (Default) value of the command key to the application executable you want to run, including any necessary parameters.
    • Example for a simple application:
      "C:\Path\To\MyApp.exe" "%1"
      

Option 2: Using a Batch Script to Add Entries

You can automate the process of adding registry entries using a batch file. Here's an example batch script that adds a new context menu entry for .txt files.

@echo off
SET KEY_NAME="HKEY_CLASSES_ROOT\txtfile\shell\OpenWithMyApp"
SET COMMAND_KEY="HKEY_CLASSES_ROOT\txtfile\shell\OpenWithMyApp\command"
SET APP_PATH="C:\Path\To\MyApp.exe"

REM Add the shell key
REG ADD %KEY_NAME% /ve /d "Open with MyApp" /f

REM Add the command key
REG ADD %COMMAND_KEY% /ve /d "\"%APP_PATH%\" \"%%1\"" /f

echo Context menu entry added successfully.

Option 3: Using PowerShell Script

You can also use PowerShell to add entries to the registry:

$FileType = "txtfile"
$MenuText = "Open with MyApp"
$AppPath = "C:\Path\To\MyApp.exe"

# Define the paths
$ShellPath = "HKCR:\$FileType\shell\$MenuText"
$CommandPath = "$ShellPath\command"

# Create the keys and set values
New-Item -Path $ShellPath -Force | Out-Null
Set-ItemProperty -Path $ShellPath -Name "(Default)" -Value $MenuText

New-Item -Path $CommandPath -Force | Out-Null
Set-ItemProperty -Path $CommandPath -Name "(Default)" -Value "`"$AppPath`" `"%1`""

Caveats and Notes

  • Administrative Privileges: Both the batch and PowerShell scripts might need to be run with administrative privileges to make changes to the registry.
  • Path Accuracy: Ensure that the path to your application executable is correct and accessible.
  • Backup: Always back up the registry before making changes.
  • Testing: After running scripts, right-click a file of the type you've modified and verify that the new context menu appears and functions as expected.

By creating these registry entries, you can customize Windows' context menus to streamline workflows and improve the user experience for specific file types.