VS Code 右键菜单修复记录

VS Code 右键菜单修复记录

autor:Aiden

问题现象

Windows 11 中右击文件夹时,第一层右键菜单里没有显示"通过 VS Code 打开"。

确认后发现:

  • 终端中执行 code . 可以正常打开 VS Code。
  • VS Code 命令路径为:D:\Program Files\Microsoft VS Code\bin\code.cmd
  • VS Code 主程序存在:D:\Program Files\Microsoft VS Code\Code.exe

因此问题不是 VS Code 无法运行,而是资源管理器右键菜单注册项缺失。

已执行的修复

创建并执行了脚本:

text 复制代码
C:\Users\yvan\Documents\Codex\2026-05-21\vscode\restore-vscode-context-menu.ps1

脚本代码:

shell 复制代码
$ErrorActionPreference = "Stop"

$codeExe = "D:\Program Files\Microsoft VS Code\Code.exe"

if (-not (Test-Path -LiteralPath $codeExe)) {

throw "VS Code executable not found: $codeExe"

}

$openWithCodeLabel = [string]::Concat(

[char]0x901A, [char]0x8FC7, " VS Code ", [char]0x6253, [char]0x5F00

)

$openHereLabel = [string]::Concat(

[char]0x5728, " VS Code ", [char]0x4E2D, [char]0x6253, [char]0x5F00, [char]0x6B64, [char]0x5904

)

$entries = @(

@{

Path = "Software\Classes\*\shell\VSCode"

Label = $openWithCodeLabel

Command = "`"$codeExe`" `"%1`""

},

@{

Path = "Software\Classes\Directory\shell\VSCode"

Label = $openWithCodeLabel

Command = "`"$codeExe`" `"%1`""

},

@{

Path = "Software\Classes\Directory\Background\shell\VSCode"

Label = $openHereLabel

Command = "`"$codeExe`" `"%V`""

}

)

$root = [Microsoft.Win32.Registry]::CurrentUser

foreach ($entry in $entries) {

$key = $root.CreateSubKey($entry.Path, $true)

$key.SetValue("", $entry.Label, [Microsoft.Win32.RegistryValueKind]::String)

$key.SetValue("MUIVerb", $entry.Label, [Microsoft.Win32.RegistryValueKind]::String)

$key.SetValue("Icon", "$codeExe,0", [Microsoft.Win32.RegistryValueKind]::String)

$key.Close()

$commandKey = $root.CreateSubKey("$($entry.Path)\command", $true)

$commandKey.SetValue("", $entry.Command, [Microsoft.Win32.RegistryValueKind]::String)

$commandKey.Close()

}

Write-Host "Done. The VS Code context menu entries were added for the current user."

脚本向当前用户注册表写入 VS Code 右键菜单项,不需要卸载或重新安装 VS Code。

写入的位置包括:

text 复制代码
HKEY_CURRENT_USER\Software\Classes\*\shell\VSCode
HKEY_CURRENT_USER\Software\Classes\Directory\shell\VSCode
HKEY_CURRENT_USER\Software\Classes\Directory\Background\shell\VSCode

对应效果:

  • 右击文件时显示"通过 VS Code 打开"
  • 右击文件夹时显示"通过 VS Code 打开"
  • 在文件夹空白处右击时显示"在 VS Code 中打开此处"

编码问题处理

第一次写入后,菜单文字显示为乱码,例如"閫氳繃 VS Code 鎵撳紑"。

原因是 Windows PowerShell 按旧编码读取脚本中的中文字符,导致写入注册表时中文被错误解析。

已将脚本改为通过 Unicode 字符码拼接中文,例如:

powershell 复制代码
$openWithCodeLabel = [string]::Concat(
    [char]0x901A, [char]0x8FC7, " VS Code ", [char]0x6253, [char]0x5F00
)

这样脚本不再依赖文件编码,重新执行后菜单文字恢复正常。

当前效果

现在右击文件夹后,点击 Windows 11 菜单底部的"显示更多选项",可以看到:

text 复制代码
通过 VS Code 打开

说明 VS Code 右键菜单已经恢复。

关于第一层新菜单

Windows 11 的第一层新右键菜单和旧版"显示更多选项"菜单不是同一套机制。

普通注册表方式只能把菜单项加到旧版完整菜单,也就是"显示更多选项"里。要直接出现在 Windows 11 第一层新菜单,通常需要应用使用新的 Shell 扩展机制,不适合用简单注册表项手动添加。

如果想让右击时直接显示完整旧菜单,可以另做 Windows 11 经典右键菜单设置。本次未执行该改动。

重新执行修复

如果以后 VS Code 更新后右键菜单又消失,可以在当前目录运行:

powershell 复制代码
powershell -NoProfile -ExecutionPolicy Bypass -File .\restore-vscode-context-menu.ps1

执行后如果资源管理器没有立刻刷新,可以重启资源管理器:

powershell 复制代码
taskkill /f /im explorer.exe
start explorer.exe

撤销方式

如需移除本次添加的右键菜单项,可以删除以下注册表项:

text 复制代码
HKEY_CURRENT_USER\Software\Classes\*\shell\VSCode
HKEY_CURRENT_USER\Software\Classes\Directory\shell\VSCode
HKEY_CURRENT_USER\Software\Classes\Directory\Background\shell\VSCode

可以使用注册表编辑器手动删除,或之后再写一个撤销脚本。

相关推荐
不好听6131 小时前
从一行 JSX 到屏幕像素:前端开发者必须懂的浏览器渲染管线
前端
恒拓高科WorkPlus1 小时前
企业级内网即时通讯建设模型:BeeWorks内网IM四层架构
前端
前端小李子2 小时前
前端环境变量裸奔?我用 EnvShield 给它穿了件防弹衣
前端
youtootech2 小时前
HarmonyOS《柚兔学伴》项目实战25-我的页面、Web 嵌入与项目总结
前端·华为·harmonyos
小林ixn2 小时前
从零到一理解 React 父子组件通信:手写一个 Todo 应用带你彻底搞懂单向数据流
前端·javascript·react.js
醇氧4 小时前
CountDownLatch / CyclicBarrier / Semaphore 面试高频问答清单
前端·面试·职场和发展
qetfw5 小时前
MXU:Tauri 2 + React 的 MaaFramework 跨平台 GUI 源码
前端·python·react.js·前端框架·开源项目·效率工具
甲维斯5 小时前
我要开始吹牛逼了!Kimi K3 “宇宙无敌”!
前端·人工智能
a1117765 小时前
微光小屋-前端养成小游戏 开源项目
前端