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

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

相关推荐
xiaofeichaichai6 小时前
Webpack
前端·webpack·node.js
问心无愧05137 小时前
ctf show web入门111
android·前端·笔记
唐某人丶7 小时前
模型越来越强,我们还需要 Agent 工程吗?—— 从价值重估到 Harness 实践
前端·agent·ai编程
智码看视界7 小时前
现代Web开发基础:全栈工程师的起航点
前端·后端·c5全栈
JS菌7 小时前
手写一个 AI Agent 全栈项目:从沙箱执行到子智能体的完整实现
前端·人工智能·后端
excel9 小时前
HLS TS 文件损坏的元凶:Git 提交与拉取
前端
Aphasia3119 小时前
https连接传输流程
前端·面试
徐小夕9 小时前
万字长文!千万级文档 RAG 知识库系统落地实践
前端·算法·github
threelab9 小时前
Three.js 物理模拟着色器 | 三维可视化 / AI 提示词
开发语言·前端·javascript·人工智能·3d·着色器