一键将Open with VScode加入鼠标右键

Friday, August 9, 2024
本文共234字
1分钟阅读时长

⚠️本文是作者P3troL1er原创,首发于https://peterliuzhi.top/posts/%E8%87%AA%E5%88%B6os%E6%97%A5%E5%BF%97/%E4%B8%80%E9%94%AE%E5%B0%86open-with-vscode%E5%8A%A0%E5%85%A5%E9%BC%A0%E6%A0%87%E5%8F%B3%E9%94%AE/。商业转载请联系作者获得授权,非商业转载请注明出处!

Always tell the truth. That way, you don’t have to remember what you said. — Mark Twain

将以下脚本保存为.bat文件并运行:

@echo off
setlocal

:: 检查管理员权限
net session >nul 2>&1
if %errorlevel% == 0 (
    echo Running with administrative privileges
    goto Start
) else (
    echo Requesting administrative privileges...
    goto UACPrompt
)

:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
    "%temp%\getadmin.vbs"
    exit /B

:Start
    :: 询问用户VSCode的安装路径
    echo Please enter the path to your Visual Studio Code executable (e.g., D:\Microsoft VS Code\Code.exe):
    set /p VSCODE_PATH=

    :: 将路径中的\替换为\\
    set "VSCODE_PATH_ESCAPED=%VSCODE_PATH:\=\\%"

    :: 更新注册表
    reg add "HKEY_CLASSES_ROOT\*\shell\VSCode" /ve /d "Open with Code" /f
    reg add "HKEY_CLASSES_ROOT\*\shell\VSCode" /v "Icon" /t REG_SZ /d "%VSCODE_PATH_ESCAPED%" /f
    reg add "HKEY_CLASSES_ROOT\*\shell\VSCode\command" /ve /d "\"%VSCODE_PATH_ESCAPED%\" \"%%1\"" /f

    reg add "HKEY_CLASSES_ROOT\Directory\shell\VSCode" /ve /d "Open with Code" /f
    reg add "HKEY_CLASSES_ROOT\Directory\shell\VSCode" /v "Icon" /t REG_SZ /d "%VSCODE_PATH_ESCAPED%" /f
    reg add "HKEY_CLASSES_ROOT\Directory\shell\VSCode\command" /ve /d "\"%VSCODE_PATH_ESCAPED%\" \"%%V\"" /f

    reg add "HKEY_CLASSES_ROOT\Directory\Background\shell\VSCode" /ve /d "Open with Code" /f
    reg add "HKEY_CLASSES_ROOT\Directory\Background\shell\VSCode" /v "Icon" /t REG_SZ /d "%VSCODE_PATH_ESCAPED%" /f
    reg add "HKEY_CLASSES_ROOT\Directory\Background\shell\VSCode\command" /ve /d "\"%VSCODE_PATH_ESCAPED%\" \"%%V\"" /f

    echo Registry updated successfully!
    pause
endlocal