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 无法运行,而是资源管理器右键菜单注册项缺失。
已执行的修复
创建并执行了脚本:
C:\Users\yvan\Documents\Codex\2026-05-21\vscode\restore-vscode-context-menu.ps1
脚本代码:
$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。
写入的位置包括:
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 字符码拼接中文,例如:
$openWithCodeLabel = [string]::Concat(
[char]0x901A, [char]0x8FC7, " VS Code ", [char]0x6253, [char]0x5F00
)
这样脚本不再依赖文件编码,重新执行后菜单文字恢复正常。
当前效果
现在右击文件夹后,点击 Windows 11 菜单底部的“显示更多选项”,可以看到:
通过 VS Code 打开
说明 VS Code 右键菜单已经恢复。
关于第一层新菜单
Windows 11 的第一层新右键菜单和旧版“显示更多选项”菜单不是同一套机制。
普通注册表方式只能把菜单项加到旧版完整菜单,也就是“显示更多选项”里。要直接出现在 Windows 11 第一层新菜单,通常需要应用使用新的 Shell 扩展机制,不适合用简单注册表项手动添加。
如果想让右击时直接显示完整旧菜单,可以另做 Windows 11 经典右键菜单设置。本次未执行该改动。
重新执行修复
如果以后 VS Code 更新后右键菜单又消失,可以在当前目录运行:
powershell -NoProfile -ExecutionPolicy Bypass -File .\restore-vscode-context-menu.ps1
执行后如果资源管理器没有立刻刷新,可以重启资源管理器:
taskkill /f /im explorer.exe
start explorer.exe
撤销方式
如需移除本次添加的右键菜单项,可以删除以下注册表项:
HKEY_CURRENT_USER\Software\Classes\*\shell\VSCode
HKEY_CURRENT_USER\Software\Classes\Directory\shell\VSCode
HKEY_CURRENT_USER\Software\Classes\Directory\Background\shell\VSCode
可以使用注册表编辑器手动删除,或之后再写一个撤销脚本。