1.需求拆解:
如何实现手动添加右键菜单的打开方式:
step1:打开注册表编辑器,win+r->输入 “regedit”
step2:在hkey_classes_root/*/shell (或者hkey_local_machine/software/classes/*/shell ,两个目录是一样的) 添加一个key:ynote,然后在该项中新建项command,然后再编辑字符串,添加应用程序的路径,最后再路径和名称的后面加上空格和“%1”,然后在右键就可以找到ynote的打开方式
2.代码实现
method1:通过_winreg模块实现:
import _winreg from _winreg import key_all_access with _winreg.openkey(_winreg.hkey_local_machine, r"software\classes\*\shell") as key: print key newkey = _winreg.createkeyex(key,"ynote",0,key_all_access) sub_key = _winreg.openkey(_winreg.hkey_local_machine,r"software\classes\*\shell\ynote") newsubkey = _winreg.createkey(sub_key,"command") _winreg.setvalue(newsubkey,"(default)",1,"\"c:\program files (x86)\youdao\youdaonote\youdaonote.exe\" \"%1\"")
method2:通过win32api和win32con模块实现
import win32api import win32con key = win32api.regopenkey(win32con.hkey_local_machine,r"software\classes\*\shell") newkey = win32api.regcreatekey(key,"ynote") sub_key = win32api.regopenkey(win32con.hkey_local_machine,r"software\classes\*\shell\ynote") newsubkey = win32api.regcreatekey(sub_key,"command") win32api.regsetvalue(newsubkey,"(default)", win32con.reg_sz,"\"c:\program files (x86)\youdao\youdaonote\youdaonote.exe\" \"%1\"")
以上就是详解python实现应用程序在右键菜单中添加打开方式步骤的详细内容。
