需要考虑32位应用程序安装在64位系统中的情况,即考虑HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall这个注册表路径。
下面直接上代码。
1 # -*- coding:utf-8 -*- 2 3 import os 4 import re 5 import winreg 6 7 ''' 8 HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall 9 HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall 10 ''' 11 12 def find_you(): 13 pass 14 15 if __name__ == '__main__': 16 pattern = re.compile(r'卸载软件关键字',re.I) 17 exclude = re.compile(r'过滤关键字',re.I) 18 paths = [ 19 'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall', 20 'SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall', 21 ] 22 root = winreg.HKEY_LOCAL_MACHINE 23 for curPath in paths: 24 try: 25 mainKey = winreg.OpenKey(root, curPath, 0, winreg.KEY_ALL_ACCESS) 26 except: 27 continue 28 subNameIndex = 0 29 while 1: 30 subName = '' 31 try: 32 subName = winreg.EnumKey(mainKey, subNameIndex) 33 subNameIndex += 1 34 except: 35 break 36 subName = subName.strip('\\') 37 if len(subName)==0: 38 continue 39 subKey = None 40 try: 41 subKey = winreg.OpenKey(root,'%s\\%s'%(curPath,subName), 0, winreg.KEY_ALL_ACCESS) 42 except: 43 continue 44 subValueIndex = 0 45 while 1: 46 try: 47 name,data,valueType = winreg.EnumValue(subKey,subValueIndex) 48 subValueIndex += 1 49 if name and name[-1]=='\\': 50 name += '\\' 51 if type(data).__name__=='list': 52 data = ''.join(data) 53 except: 54 break 55 if name=='DisplayName' and (re.search(pattern,data) and not re.search(exclude,data)): 56 targetValueIndex = 0 57 while 1: 58 try: 59 name,data,valueType = winreg.EnumValue(subKey,targetValueIndex) 60 targetValueIndex += 1 61 if name and name[-1]=='\\': 62 name += '\\' 63 if type(data).__name__=='list': 64 data = ''.join(data) 65 except: 66 break 67 if name=='UninstallString': 68 result = re.findall(re.compile(r'(\{[\d\D]*?\})'),data) 69 uninst = re.findall(re.compile(r'(C:.*?uninst.*?exe)'),data) 70 cmd = 'dir' 71 if len(result)>0: 72 cmd = 'msiexec /X %s /quiet /norestart'%result[0] 73 if len(uninst)>0: 74 cmd = 'msiexec /X "%s" /quiet /norestart'%uninst[0] 75 print(cmd) 76 p = os.popen(cmd) 77 p.read() 78 p.close()
浙公网安备 33010602011771号