python实现应用程序在右键菜单中添加打开方式

    最近项目组开发的一个小工具想要在右键菜单中添加打开方式,以有道云笔记为例进行了需求拆解和代码编写

1.需求拆解:

如何实现手动添加右键菜单的打开方式:

Step1:打开注册表编辑器,Win+R->输入 “regedit”

Step2:在HKEY_CLASSES_ROOT/*/shell (或者HKEY_LOCAL_MACHINE/SOFTWARE/Classes/*/shell ,两个目录是一样的) 添加一个key:YNote,然后在该项中新建项command,然后再编辑字符串,添加应用程序的路径,最后再路径和名称的后面加上空格和“%1”,然后在右键就可以找到YNote的打开方式

2.代码实现

Method1:通过_winreg模块实现:

 1 import _winreg
 2 from _winreg import KEY_ALL_ACCESS 
 3 
 4 with _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\Classes\*\shell") as key:
 5     print key
 6 
 7     newKey = _winreg.CreateKeyEx(key,"YNote",0,KEY_ALL_ACCESS)
 8     
 9     sub_key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,r"SOFTWARE\Classes\*\shell\YNote")
10     newsubKey = _winreg.CreateKey(sub_key,"command")
11     
12     _winreg.SetValue(newsubKey,"(Default)",1,"\"C:\Program Files (x86)\Youdao\YoudaoNote\YoudaoNote.exe\" \"%1\"")
View Code

 

 

Method2:通过win32api和win32con模块实现

 1 import win32api
 2 import win32con
 3 
 4 key = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE,r"SOFTWARE\Classes\*\shell")
 5     
 6 newKey = win32api.RegCreateKey(key,"YNote")
 7     
 8 sub_key = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE,r"SOFTWARE\Classes\*\shell\YNote")
 9 
10 newsubKey = win32api.RegCreateKey(sub_key,"command")
11     
12 win32api.RegSetValue(newsubKey,"(Default)", win32con.REG_SZ,"\"C:\Program Files (x86)\Youdao\YoudaoNote\YoudaoNote.exe\" \"%1\"")
View Code

 

posted @ 2017-01-09 14:54  陈年燕酒  阅读(1769)  评论(3编辑  收藏  举报