windows11 如何支持增加右键通过vscode打开文件夹

AI 生成的摘要

doggie provides a detailed guide to add ‘Open with Visual Studio Code’ to the Windows 11 right-click context menu for folders—both in folder background and directly on folders. The solution uses PowerShell to modify HKEY_CURRENT_USER registry keys, ensuring no admin privileges are needed and affecting only the current user. It covers detecting VS Code’s install path (C:\Users\www\AppData\Local\Programs\Microsoft VS Code\Code.exe), setting up two registry entries (one under Directory\Background\shell\VSCode, another under Directory\shell\VSCode), using %V for background clicks and %1 for direct folder clicks, and adding the VS Code icon via the Icon registry value. A complete, ready-to-run PowerShell script is included for setup and cleanup (with Remove-Item commands to uninstall). Notes clarify Windows 11’s ‘Show more options’ behavior and troubleshooting steps like restarting File Explorer or logging out.

Windows 11 添加“使用 Visual Studio Code 打开”右键菜单

实现效果

设置完成后,可以通过以下两种方式用 VS Code 打开目录:

  1. 打开某个文件夹,在空白处单击右键,选择“使用 Visual Studio Code 打开”。
  2. 直接右键单击一个文件夹,选择“使用 Visual Studio Code 打开”。

Windows 11 可能会把传统程序的菜单项放在“显示更多选项”中。也可以按 Shift + F10 打开完整右键菜单。

我是怎么设置的

我先确认了 VS Code 的实际安装位置:

C:\Users\www\AppData\Local\Programs\Microsoft VS Code\Code.exe

然后在当前用户的注册表中创建了两个菜单项:

使用场景 注册表位置 传给 VS Code 的路径参数
在文件夹空白处右键 HKEY_CURRENT_USER\Software\Classes\Directory\Background\shell\VSCode %V,表示当前打开的目录
直接右键文件夹 HKEY_CURRENT_USER\Software\Classes\Directory\shell\VSCode %1,表示被单击的文件夹

使用 HKEY_CURRENT_USER 只会影响当前 Windows 用户,通常不需要管理员权限。

一键设置命令

打开 PowerShell,粘贴并执行下面的命令:

$codeExe = "$env:LOCALAPPDATA\Programs\Microsoft VS Code\Code.exe"

if (-not (Test-Path -LiteralPath $codeExe)) {
    throw "找不到 VS Code:$codeExe。请先确认 VS Code 的安装位置。"
}

# 在已打开文件夹的空白处右键
$backgroundKey = 'Registry::HKEY_CURRENT_USER\Software\Classes\Directory\Background\shell\VSCode'
$backgroundCommandKey = "$backgroundKey\command"

New-Item -Path $backgroundCommandKey -Force | Out-Null
Set-Item -LiteralPath $backgroundKey -Value '使用 Visual Studio Code 打开'
New-ItemProperty `
    -LiteralPath $backgroundKey `
    -Name 'Icon' `
    -Value ('"' + $codeExe + '"') `
    -PropertyType String `
    -Force | Out-Null
Set-Item -LiteralPath $backgroundCommandKey -Value ('"' + $codeExe + '" "%V"')

# 直接右键一个文件夹
$directoryKey = 'Registry::HKEY_CURRENT_USER\Software\Classes\Directory\shell\VSCode'
$directoryCommandKey = "$directoryKey\command"

New-Item -Path $directoryCommandKey -Force | Out-Null
Set-Item -LiteralPath $directoryKey -Value '使用 Visual Studio Code 打开'
New-ItemProperty `
    -LiteralPath $directoryKey `
    -Name 'Icon' `
    -Value ('"' + $codeExe + '"') `
    -PropertyType String `
    -Force | Out-Null
Set-Item -LiteralPath $directoryCommandKey -Value ('"' + $codeExe + '" "%1"')

Write-Host '设置完成。' -ForegroundColor Green

命令的关键部分

以文件夹空白处的菜单为例,最终启动命令是:

"C:\Users\www\AppData\Local\Programs\Microsoft VS Code\Code.exe" "%V"
  • 第一部分是 VS Code 主程序的完整路径。
  • %V 会由 Windows 替换为当前文件夹路径。
  • 路径两侧加双引号,可以正确处理名称中含空格的文件夹。
  • Icon 属性让右键菜单显示 VS Code 图标。

直接右键文件夹时,命令使用 %1,Windows 会把被单击文件夹的路径传给 VS Code。

验证设置

  1. 打开文件资源管理器并进入任意文件夹。
  2. 在空白处单击右键。
  3. 如果第一层菜单没有显示该选项,选择“显示更多选项”。
  4. 单击“使用 Visual Studio Code 打开”,确认 VS Code 打开了当前文件夹。
  5. 再右键任意文件夹,测试第二个菜单入口。

注册表菜单通常会立即生效。如果没有出现,可以关闭并重新打开文件资源管理器;仍未出现时,再注销并重新登录 Windows。

删除这些菜单项

如果以后不再需要,可以在 PowerShell 中执行:

Remove-Item `
    -LiteralPath 'Registry::HKEY_CURRENT_USER\Software\Classes\Directory\Background\shell\VSCode' `
    -Recurse `
    -Force `
    -ErrorAction SilentlyContinue

Remove-Item `
    -LiteralPath 'Registry::HKEY_CURRENT_USER\Software\Classes\Directory\shell\VSCode' `
    -Recurse `
    -Force `
    -ErrorAction SilentlyContinue

Write-Host 'VS Code 右键菜单已删除。' -ForegroundColor Green

这些删除命令只移除本教程添加的两个 VSCode 注册表项,不会卸载 VS Code,也不会删除项目文件。

另一种设置方法

如果重新运行 VS Code 官方安装程序,可以勾选类似以下选项:

  • 将“通过 Code 打开”操作添加到 Windows 资源管理器文件目录上下文菜单。
  • 将“通过 Code 打开”操作添加到 Windows 资源管理器文件上下文菜单。

不过,本教程使用注册表方式,不需要重新安装 VS Code,并且可以分别控制文件夹空白处和文件夹本身的菜单入口。