一键将Open with Code加入鼠标右键-BAT脚本

Thursday, July 18, 2024
本文共337字
1分钟阅读时长

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

The key is to keep company only with people who uplift you, whose presence calls forth your best. — Epictetus

引言

在退VScode的版本的时候不知道为什么右键的Open with Code选项消失了,导致打开vscode很麻烦。查阅了一些资料后,写了一个BAT脚本用于一键添加VScode到右键

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

中间会询问你的vscode路径,复制粘贴你的路径就行