[vscode] vscode右键修复

📅 发布时间:2026/7/5 5:37:00 👁️ 浏览次数:
[vscode] vscode右键修复
我找了很久的问题,如何修改vscode右键菜单丢失的问题,很多都是自己手动调节, 这样很费劲, 我不是很喜欢, 现在编写一个脚本来快速完成此问题, 从而避免手动操作带来的错误下方为对应的代码#!/usr/bin/env python3 Diagnose and repair VS Code context menu entries on Windows. Use cases: 1) Check whether Open with Code exists for folder background right-click. 2) Repair missing/broken entries by writing per-user registry keys. This script writes to HKCU\\Software\\Classes so admin rights are usually not required. 修复 Windows 上 VS Code 右键菜单项的诊断和修复。 from__future__importannotationsimportosimportplatformimportsysfromdataclassesimportdataclassfromtypingimportList,Optional,Tupletry:importwinregexceptImportErrorasexc:raiseSystemExit(This script only supports Windows (winreg is unavailable).)fromexc MENU_NAMEVSCodeMENU_TEXTOpen with CodedataclassclassCheckItem:key_path:strexists:boolmenu_text:Optional[str]command:Optional[str]issue:Optional[str]Nonedefis_windows()-bool:returnplatform.system().lower()windowsdefcandidate_code_paths()-List[str]:localos.environ.get(LOCALAPPDATA,)program_filesos.environ.get(ProgramFiles,)program_files_x86os.environ.get(ProgramFiles(x86),)candidates[os.path.join(local,Programs,Microsoft VS Code,Code.exe),os.path.join(program_files,Microsoft VS Code,Code.exe),os.path.join(program_files_x86,Microsoft VS Code,Code.exe),]# Keep order, remove empty/duplicates.seenset()ordered[]forpathincandidates:normos.path.normcase(path)ifpathandnormnotinseen:seen.add(norm)ordered.append(path)returnordereddefget_code_path_from_app_paths()-Optional[str]:app_paths_subkeyrSOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\Code.exeforrootin(winreg.HKEY_CURRENT_USER,winreg.HKEY_LOCAL_MACHINE):try:withwinreg.OpenKey(root,app_paths_subkey,0,winreg.KEY_READ)askey:value,_winreg.QueryValueEx(key,)ifvalueandos.path.exists(value):returnvalueexceptFileNotFoundError:continueexceptOSError:continuereturnNonedefresolve_code_exe()-Optional[str]:from_registryget_code_path_from_app_paths()iffrom_registry:returnfrom_registryforpathincandidate_code_paths():ifos.path.exists(path):returnpathreturnNonedefread_string_value(root,path:str,value_name:str)-Optional[str]:try:withwinreg.OpenKey(root,path,0,winreg.KEY_READ)askey:value,_winreg.QueryValueEx(key,value_name)returnstr(value)exceptFileNotFoundError:returnNoneexceptOSError:returnNonedefcheck_menu_key(menu_key_path:str)-CheckItem:menu_textread_string_value(winreg.HKEY_CURRENT_USER,menu_key_path,)command_pathmenu_key_pathr\commandcommandread_string_value(winreg.HKEY_CURRENT_USER,command_path,)existsmenu_textisnotNoneorcommandisnotNoneissueNoneifnotexists:issueMissing keyelifnotcommand:issueMissing command valueelifCode.exenotincommandandcode.exenotincommand:issueCommand does not point to Code.exereturnCheckItem(key_pathmenu_key_path,existsexists,menu_textmenu_text,commandcommand,issueissue,)defcheck_configuration()-List[CheckItem]:baserSoftware\Classeschecks[check_menu_key(base\\Directory\\Background\\shell\\MENU_NAME),check_menu_key(base\\Directory\\shell\\MENU_NAME),]returnchecksdefcreate_or_update_menu(root,menu_key_path:str,menu_text:str,icon_path:str,command_value:str)-None:withwinreg.CreateKeyEx(root,menu_key_path,0,winreg.KEY_SET_VALUE)asmenu_key:winreg.SetValueEx(menu_key,,0,winreg.REG_SZ,menu_text)winreg.SetValueEx(menu_key,Icon,0,winreg.REG_SZ,icon_path)withwinreg.CreateKeyEx(root,menu_key_pathr\command,0,winreg.KEY_SET_VALUE)ascmd_key:winreg.SetValueEx(cmd_key,,0,winreg.REG_SZ,command_value)deffix_configuration(code_exe:str)-Tuple[bool,List[str]]:messages[]rootwinreg.HKEY_CURRENT_USER baserSoftware\Classesbg_menubase\\Directory\\Background\\shell\\MENU_NAME dir_menubase\\Directory\\shell\\MENU_NAME bg_cmdf{code_exe} %Vdir_cmdf{code_exe} %1try:create_or_update_menu(root,bg_menu,MENU_TEXT,code_exe,bg_cmd)messages.append(fUpdated: HKCU\\{bg_menu})create_or_update_menu(root,dir_menu,MENU_TEXT,code_exe,dir_cmd)messages.append(fUpdated: HKCU\\{dir_menu})returnTrue,messagesexceptPermissionError:messages.append(Permission denied when writing registry keys.)returnFalse,messagesexceptOSErrorasexc:messages.append(fRegistry write failed:{exc})returnFalse,messagesdefprint_check_report(items:List[CheckItem],code_exe:Optional[str])-bool:print( VS Code Context Menu Check )print(fOS:{platform.platform()})print(fDetected Code.exe:{code_exeorNOT FOUND})print()all_okTrueforiteminitems:statusOKifitem.issue:statusISSUEall_okFalseprint(f[{status}]{item.key_path})print(f Menu Text:{item.menu_text!r})print(f Command :{item.command!r})ifitem.issue:print(f Problem :{item.issue})print()returnall_okdefmain()-int:ifnotis_windows():print(This script only supports Windows.)return2code_exeresolve_code_exe()check_itemscheck_configuration()all_okprint_check_report(check_items,code_exe)ifall_ok:print(No issues found.)return0print(Issues detected. Starting automatic repair...)ifnotcode_exe:print(Cannot fix because Code.exe was not found.)print(Please install VS Code first, then run this script again.)return3print(Applying fixes...)ok,messagesfix_configuration(code_exe)formsginmessages:print(f-{msg})ifnotok:return4print(Repair completed. Re-checking...)verify_itemscheck_configuration()verify_okprint_check_report(verify_items,code_exe)ifverify_ok:print(Context menu repair succeeded.)print(If Explorer does not update immediately, restart Explorer or sign out/in.)return0print(Repair attempted, but some issues remain.)return5if__name____main__:sys.exit(main())