如何为Windows右键菜单添加Fleet IDE的「用Fleet打开文件夹」选项
解决Fleet IDE右键菜单无法启动的问题
问题描述
我尝试给Fleet IDE添加右键菜单,用来打开文件夹,试过Bat、PowerShell、Python编写注册表项,但都没成功。注册表项在Regedit里能看到,但点击后无法启动应用,附上了我的Bat脚本,求Bat或其他语言的实现指引。
原Bat脚本:
@echo off setlocal enabledelayedexpansion :: Function to check if the script is running as administrator :check_admin net session >nul 2>&1 if %errorLevel% neq 0 ( echo Running without administrative privileges. Reopening with elevated privileges... set "script_path=%~0" set "params=%*" set "batch_cmd=^ set params=!params:*%~0=! set params=!params:~1! call "%~0" !params! %* exit /b " powershell Start-Process -Verb RunAs -FilePath "cmd.exe" -ArgumentList "/c %batch_cmd%" exit /b ) exit /b :: Rest of your script here :main :: Your existing script logic goes here :: Call the check_admin function at the beginning call :check_admin :: Continue with the main script logic echo "This script is running with administrative privileges." :: Define paths and menu text set "menu_text=Open with Fleet" set "app_path=C:\Users\dower\AppData\Local\Programs\Fleet\Fleet.exe" set "icon_path=C:\Users\dower\AppData\Local\Programs\Fleet\Fleet.ico" :: Function to add a new context menu entry for folders :add_context_menu reg add "HKEY_CLASSES_ROOT\Directory\Background\shell\%menu_text%" /ve /d "%menu_text%" /f reg add "HKEY_CLASSES_ROOT\Directory\Background\shell\%menu_text%\command" /ve /d "\"%app_path%\" --dir \"%%1\"" /f reg add "HKEY_CLASSES_ROOT\Directory\Background\shell\%menu_text%\Icon" /ve /d "%app_path%,0" /f exit /b :: Function to remove the existing context menu entry :remove_context_menu reg delete "HKEY_CLASSES_ROOT\Directory\Background\shell\%menu_text%" /f exit /b :: Main script :: Check if the existing entry needs to be removed for /f "tokens=* delims=" %%a in ('reg query "HKEY_CLASSES_ROOT\Directory\Background\shell" ^| find /i "%menu_text%"') do ( set "existing_entry=%%a" ) if defined existing_entry ( call :remove_context_menu ) :: Add a new context menu entry call :add_context_menu echo Successfully added '%menu_text%' context menu entry for folders.
问题分析
你的脚本存在几个关键问题导致右键点击无法启动Fleet:
- 管理员权限检查未触发:脚本定义了
:main标签但从未执行,导致权限检查逻辑完全没运行,注册表写入可能因权限不足仅生效于当前用户 hive,而非系统级 hive。 - 右键参数错误:
Directory\Background(桌面/文件夹背景右键)的路径参数变量应为%V,而不是%1,%1仅适用于直接右键点击文件夹的场景。 - 权限重启逻辑冗余:原脚本的PowerShell重启逻辑存在参数传递问题,可能导致重启后的脚本无法正常执行。
修正后的Bat脚本
@echo off setlocal enabledelayedexpansion :: 检查管理员权限 net session >nul 2>&1 if %errorLevel% neq 0 ( echo 正在以管理员权限重启脚本... powershell -Command "Start-Process cmd.exe -ArgumentList '/c ""%~0""' -Verb RunAs" exit /b ) :: 配置参数 set "menu_text=Open with Fleet" set "app_path=C:\Users\dower\AppData\Local\Programs\Fleet\Fleet.exe" :: 移除旧条目(避免残留) reg delete "HKEY_CLASSES_ROOT\Directory\Background\shell\%menu_text%" /f 2>nul reg delete "HKEY_CLASSES_ROOT\Directory\shell\%menu_text%" /f 2>nul :: 添加文件夹背景右键条目 reg add "HKEY_CLASSES_ROOT\Directory\Background\shell\%menu_text%" /ve /d "%menu_text%" /f reg add "HKEY_CLASSES_ROOT\Directory\Background\shell\%menu_text%" /v "Icon" /d "\"%app_path%\",0" /f reg add "HKEY_CLASSES_ROOT\Directory\Background\shell\%menu_text%\command" /ve /d "\"%app_path%\" --dir \"%%V\"" /f :: 添加直接右键文件夹条目(覆盖更多使用场景) reg add "HKEY_CLASSES_ROOT\Directory\shell\%menu_text%" /ve /d "%menu_text%" /f reg add "HKEY_CLASSES_ROOT\Directory\shell\%menu_text%" /v "Icon" /d "\"%app_path%\",0" /f reg add "HKEY_CLASSES_ROOT\Directory\shell\%menu_text%\command" /ve /d "\"%app_path%\" --dir \"%%1\"" /f echo 已成功添加'%menu_text%'右键菜单条目! pause exit /b
修改说明
- 简化管理员权限检查逻辑,直接用PowerShell重启脚本,避免参数传递错误。
- 同时添加文件夹背景右键和直接右键文件夹两种条目,覆盖所有常用场景。
- 修正参数变量:背景右键用
%V,直接右键文件夹用%1。 - 先删除旧条目再添加,避免注册表残留导致的异常。
PowerShell实现(更简洁)
# 检查管理员权限 $currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent()) if (-not $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { Start-Process powershell.exe "-File `"$PSCommandPath`"" -Verb RunAs exit } $menuText = "Open with Fleet" $appPath = "C:\Users\dower\AppData\Local\Programs\Fleet\Fleet.exe" # 删除现有条目 Remove-Item -Path "HKCR:\Directory\Background\shell\$menuText" -Recurse -Force -ErrorAction SilentlyContinue Remove-Item -Path "HKCR:\Directory\shell\$menuText" -Recurse -Force -ErrorAction SilentlyContinue # 添加文件夹背景右键条目 New-Item -Path "HKCR:\Directory\Background\shell\$menuText" -Force | Out-Null Set-ItemProperty -Path "HKCR:\Directory\Background\shell\$menuText" -Name "(Default)" -Value $menuText Set-ItemProperty -Path "HKCR:\Directory\Background\shell\$menuText" -Name "Icon" -Value "`"$appPath`",0" New-Item -Path "HKCR:\Directory\Background\shell\$menuText\command" -Force | Out-Null Set-ItemProperty -Path "HKCR:\Directory\Background\shell\$menuText\command" -Name "(Default)" -Value "`"$appPath`" --dir `"%V`"" # 添加直接右键文件夹条目 New-Item -Path "HKCR:\Directory\shell\$menuText" -Force | Out-Null Set-ItemProperty -Path "HKCR:\Directory\shell\$menuText" -Name "(Default)" -Value $menuText Set-ItemProperty -Path "HKCR:\Directory\shell\$menuText" -Name "Icon" -Value "`"$appPath`",0" New-Item -Path "HKCR:\Directory\shell\$menuText\command" -Force | Out-Null Set-ItemProperty -Path "HKCR:\Directory\shell\$menuText\command" -Name "(Default)" -Value "`"$appPath`" --dir `"%1`"" Write-Host "已成功添加'$menuText'右键菜单条目!" Read-Host "按任意键退出"
内容的提问来源于stack exchange,提问作者AlexanderLamilton
相关产品推荐
相关产品推荐

