[Python] 纯文本查看 复制代码
#encoding:utf-8
import winreg
import re
def get_ParentKeyName(next_keyname):
StdUninstallKey=r'Software\Microsoft\Windows\CurrentVersion\Uninstall'
key_path = StdUninstallKey + '\\' + next_keyname
try:
final_keypath = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, key_path)
ParentKeyName = winreg.QueryValueEx(final_keypath, 'ParentKeyName')
return ParentKeyName[0]
#print(ParentKeyName[0])
except:
#print("出错了")
pass
def GetInstallerKeyNameFromGuid(GuidName):
list01=GuidName.replace("{","").replace("}","").split("-")
list02=[]
for i in range(3):
list02.append(list01[i][::-1])
for z in range(3,5):
for j in range(0,len(list01[z][1::2])):
list02.append(list01[z][1::2][j])
list02.append(list01[z][0::2][j])
#print(list02)
return ''.join(list02)
def get_localsoft(key,zhucebiao):
ClassesKey = r'Software\Classes\Installer\Products'
for num in range(0,winreg.QueryInfoKey(key)[0]):
SystemComponent = 0
next_keyname=winreg.EnumKey(key,num) #最后一级路径
key_path=zhucebiao+'\\'+next_keyname
final_keypath=winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE,key_path)
try:
SystemComponent,REG_SZ=winreg.QueryValueEx(final_keypath,'SystemComponent')
except WindowsError:
pass
if SystemComponent != 1:
for i in range(0,winreg.QueryInfoKey(final_keypath)[1]):
WindowsInstaller=0
try:
WindowsInstaller, REG_SZ = winreg.QueryValueEx(final_keypath, 'WindowsInstaller')
#print(type(WindowsInstaller))
except WindowsError:
pass
if WindowsInstaller != 1:
ReleaseType=""
#ParentKeyName=""
try:
ReleaseType, REG_SZ = winreg.QueryValueEx(final_keypath, 'ReleaseType')
#ParentKeyName, REG_SZ = winreg.QueryValueEx(final_keypath, 'ParentKeyName')
except WindowsError:
pass
#if(re.match(r'KB[0-9]{6}$',next_keyname) is None or not ReleaseType=="Security Update" or not ReleaseType=="Update Rollup" or not ReleaseType=="Hotfix" or ParentKeyName):
if re.search(r'KB[0-9]{6}$',next_keyname) or ReleaseType == "Security Update" or ReleaseType == "Update Rollup" or ReleaseType == "Hotfix" or get_ParentKeyName(next_keyname):
pass
else:
UninstallStringExists=False
DisplayName=""
for i in range(0, winreg.QueryInfoKey(final_keypath)[1]):
zijian = winreg.EnumValue(final_keypath, i)[0]
if zijian=='UninstallString':
UninstallStringExists = True
break
if UninstallStringExists == True:
try:
DisplayName, REG_SZ = winreg.QueryValueEx(final_keypath, 'DisplayName')
except WindowsError:
pass
softname=str(DisplayName)
if softname and not softname in ExistingProgramList:
ExistingProgramList.append(softname)
else:
Name=""
MsiKeyName=GetInstallerKeyNameFromGuid(next_keyname)
Class_path = ClassesKey + '\\' + MsiKeyName
try:
Class_keypath = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, Class_path)
Name, REG_SZ = winreg.QueryValueEx(Class_keypath, 'ProductName')
except WindowsError:
pass
if Name and not Name in ExistingProgramList:
ExistingProgramList.append(Name)
def get_usersoft(key,zhucebiao):
ClassesKey = r'Software\Classes\Installer\Products'
for num in range(0,winreg.QueryInfoKey(key)[0]):
SystemComponent = 0
next_keyname=winreg.EnumKey(key,num) #最后一级路径
key_path=zhucebiao+'\\'+next_keyname
final_keypath=winreg.OpenKey(winreg.HKEY_USERS,key_path)
try:
SystemComponent,REG_SZ=winreg.QueryValueEx(final_keypath,'SystemComponent')
except WindowsError:
pass
if SystemComponent != 1:
for i in range(0,winreg.QueryInfoKey(final_keypath)[1]):
WindowsInstaller=0
try:
WindowsInstaller, REG_SZ = winreg.QueryValueEx(final_keypath, 'WindowsInstaller')
#print(type(WindowsInstaller))
except WindowsError:
pass
if WindowsInstaller != 1:
ReleaseType=""
ParentKeyName=""
try:
ReleaseType, REG_SZ = winreg.QueryValueEx(final_keypath, 'ReleaseType')
ParentKeyName, REG_SZ = winreg.QueryValueEx(final_keypath, 'ParentKeyName')
except WindowsError:
pass
#if re.search(r'KB[0-9]{6}$',next_keyname) or not ReleaseType=="Security Update" or not ReleaseType=="Update Rollup" or not ReleaseType=="Hotfix" or ParentKeyName or not ReleaseType=="Update":
if re.search(r'KB[0-9]{6}$',next_keyname) or ReleaseType == "Security Update" or ReleaseType == "Update Rollup" or ReleaseType == "Hotfix" or get_ParentKeyName(next_keyname):
pass
else:
UninstallStringExists=False
DisplayName=""
for i in range(0, winreg.QueryInfoKey(final_keypath)[1]):
zijian = winreg.EnumValue(final_keypath, i)[0]
if zijian=='UninstallString':
UninstallStringExists = True
break
if UninstallStringExists == True:
try:
DisplayName, REG_SZ = winreg.QueryValueEx(final_keypath, 'DisplayName')
except WindowsError:
pass
softname=str(DisplayName)
if softname and not softname in ExistingProgramList:
ExistingProgramList.append(softname)
else:
Name=""
MsiKeyName=GetInstallerKeyNameFromGuid(next_keyname)
Class_path = ClassesKey + '\\' + next_keyname
try:
Class_keypath = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, Class_path)
Name, REG_SZ = winreg.QueryValueEx(final_keypath, 'ProductName')
except WindowsError:
pass
if Name and not Name in ExistingProgramList:
ExistingProgramList.append(Name)
def user_softget(CuInstallKey):
for usernum in range(0, winreg.QueryInfoKey(winreg.HKEY_USERS)[0]):
SystemComponent = 0
UserSid = winreg.EnumKey(winreg.HKEY_USERS, usernum) # 第一级路径
# print(UserSid)
key_path01 = UserSid + '\\' + CuInstallKey
try:
product_path = winreg.OpenKey(winreg.HKEY_USERS, key_path01)
except:
continue
for product_num in range(0, winreg.QueryInfoKey(product_path)[0]):
ProductFound = False
CuProductGuid = str(winreg.EnumKey(product_path, product_num))
key_path02 = r'Software\Microsoft\Windows\CurrentVersion\Installer\UserData'
userdata_path = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, key_path02)
for userdata_num in range(0, winreg.QueryInfoKey(userdata_path)[0]):
UserDataKeyName = winreg.EnumKey(userdata_path, userdata_num)
if UserDataKeyName != "S-1-5-18":
ProductsKey = key_path02 + "\\" + UserDataKeyName + "\\Products"
try:
pro_path = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, ProductsKey)
except:
continue
for a in range(0, winreg.QueryInfoKey(pro_path)[0]):
LmProductGuid = str(winreg.EnumKey(pro_path, a))
if LmProductGuid == CuProductGuid:
SystemComponent = 0
UserDataProgramKey = ProductsKey + "\\" + LmProductGuid + "\\InstallProperties"
UserDataProgramKey_path = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, UserDataProgramKey)
try:
SystemComponent, REG_SZ = winreg.QueryValueEx(UserDataProgramKey_path,
'SystemComponent')
except:
pass
if SystemComponent != 1:
Name = ""
ProName = key_path01 + "\\" + CuProductGuid
ProName_path = winreg.OpenKey(winreg.HKEY_USERS, ProName)
try:
Name, REG_SZ = winreg.QueryValueEx(ProName_path, 'ProductName')
except:
pass
if Name and not Name in ExistingProgramList:
ExistingProgramList.append(Name)
if __name__ == "__main__":
#Wow64UninstallKey=r'SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall'
#ClassesKey=r'Software\Classes\Installer\Products'
#StdUninstallKey=r'Software\Microsoft\Windows\CurrentVersion\Uninstall'
CuUnInstallKey=r'Software\Microsoft\Windows\CurrentVersion\Uninstall'
CuInstallKey = r'Software\Microsoft\Installer\Products'
ExistingProgramList = []
list001=[r'SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall',r'Software\Microsoft\Windows\CurrentVersion\Uninstall']
for i in list001:
key = winreg.OpenKeyEx(winreg.HKEY_LOCAL_MACHINE, i)
get_localsoft(key,i)
#key = winreg.OpenKeyEx(winreg.HKEY_LOCAL_MACHINE, StdUninstallKey)
#get_localsoft(key, StdUninstallKey)
for usernum in range(0, winreg.QueryInfoKey(winreg.HKEY_USERS)[0]):
UserSid = winreg.EnumKey(winreg.HKEY_USERS, usernum) # 第一级路径
# print(UserSid)
key_path01 = UserSid + '\\' + CuUnInstallKey
try:
key = winreg.OpenKeyEx(winreg.HKEY_USERS, key_path01)
get_usersoft(key, key_path01)
except:
continue
user_softget(CuInstallKey)
ExistingProgramList.sort()
#print(ExistingProgramList)
#print("去除重名软件,共{}项".format(len(ExistingProgramList)))
with open("D:/GetSoftware.txt",'w') as f:
f.write("去除重名软件,共{}项".format(len(ExistingProgramList)))
f.write('\n')
for soft in ExistingProgramList:
f.write(soft)
f.write('\n')
#print(sorted(ExistingProgramList), len(ExistingProgramList))
#QueryInfoKey(key)返回3元素元组(下一级路径总数,子健的数量,最后修改时间),
# 当没有下一级路径,第一个元素为0,当有下一级路径,第二个元素为0,最后修改时间为自1601年1月1日以来的100纳秒