最近項目組開發的一個小工具想要在右鍵菜單中添加打開方式,以有道云筆記為例進行了需求拆解和代碼編寫
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
2
3
4
5
6
7
8
|
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模塊實現
1
2
3
4
5
6
7
|
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實現應用程序在右鍵菜單中添加打開方式功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:http://www.cnblogs.com/candychen20170109/archive/2017/01/09/6265272.html