1.使用python添加注册表key。
# -*- coding: utf-8 -*-
"""
Created on Sat May 1 00:18:14 2021
@author: weiyi
"""
import os
import win32api
import win32con
DIR = []
DIR.append(u'C:\Program Files')
DIR.append(u'C:\Program Files (x86)')
DIR.append(u'D:\Program Files')
DIR.append(u'D:\Program Files (x86)')
DIR.append(u'E:\Program Files')
DIR.append(u'E:\Program Files (x86)')
DIR.append(u'F:\Program Files')
DIR.append(u'F:\Program Files (x86)')
name = 'sublime_text.exe'
def search(path,name):
depth = 0
for root,dirs,files in os.walk(path):
depth = depth + 1
if depth == 3:
break
for f in files:
if f.endswith(name):
fullname = os.path.join(root, f)
return fullname
pass
pass
pass
for d in DIR:
print('start search',d)
r = search(d, name)
if r != None:
break
dir_str = ''
if r != None:
print('find',name,r)
print('Use this path:(Y/N)')
use = input()
if 'Y' in use or 'yes' in use or'YES' in use or 'y' in use or 'Yes' in use:
dir_str = r
if r == None or dir_str == '':
print('find no result',name,'input path:')
dir_str = input()
if dir_str != '':
key = win32api.RegOpenKey(win32con.HKEY_CLASSES_ROOT,'*\\shell',0,win32con.KEY_ALL_ACCESS)
win32api.RegCreateKey(key,'sublime')
win32api.RegCloseKey(key)
key = win32api.RegOpenKey(win32con.HKEY_CLASSES_ROOT,'*\\shell\\sublime',0,win32con.KEY_ALL_ACCESS)
win32api.RegSetValueEx(key,'Icon',0,win32con.REG_SZ,dir_str+',0')
win32api.RegSetValue(key,'command',win32con.REG_SZ,dir_str+' %1')
win32api.RegCloseKey(key)
key = win32api.RegOpenKey(win32con.HKEY_CLASSES_ROOT,'\\directory\\shell',0,win32con.KEY_ALL_ACCESS)
win32api.RegCreateKey(key,'用sublime打开')
win32api.RegCloseKey(key)
key = win32api.RegOpenKey(win32con.HKEY_CLASSES_ROOT,'\\directory\\shell\\用sublime打开',0,win32con.KEY_ALL_ACCESS)
win32api.RegSetValueEx(key,'Icon',0,win32con.REG_SZ,dir_str+',0')
win32api.RegSetValue(key,'command',win32con.REG_SZ,dir_str+' %1')
win32api.RegCloseKey(key)