reg query "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts.txt\UserChoice" /v ProgId
解析得到
HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts.txt\UserChoice
ProgId REG_SZ AppX4ztfk9wxr86nxmzzq47px0nh0e58b8fw
得到
拼接新的查询
reg add "HKEY_CLASSES_ROOT\AppX4ztfk9wxr86nxmzzq47px0nh0e58b8fw\DefaultIcon" /t REG_SZ /d "D:\Access11.png" /f
这个是带键值的设置方式
reg add "HKEY_CLASSES_ROOT\AppX4ztfk9wxr86nxmzzq47px0nh0e58b8fw\DefaultIcon" /v "default" /t REG_SZ /d "D:\Access11.png" /f
import subprocess
# 查询注册表获取 ProgId 值
query_command = r'reg query "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.txt\UserChoice" /v ProgId'
result = subprocess.run(query_command, stdout=subprocess.PIPE, shell=True)
output = result.stdout.decode('utf-8')
# 解析得到 ProgId 值
lines = output.split('\n')
prog_id = None
for line in lines:
if 'ProgId' in line:
prog_id = line.split()[-1]
break
if prog_id:
print(f"得到 ProgId 值: {prog_id}")
new_reg_command = r'reg add "HKEY_CLASSES_ROOT\{0}\DefaultIcon" /t REG_SZ /d "D:\\Access1-1.png" /f'.format(prog_id)
print(new_reg_command)
subprocess.run(new_reg_command, shell=True)
else:
print("未能获取 ProgId 值")