我试图制作一个在Windows环境中持久化的python脚本。我使用PyInstaller来创建一个exe文件。我设法让这个脚本只能在Windows XP环境下运行,而不能在任何其他版本的Windows上运行。我可以将该exe文件移动到%temp%文件夹,但它不会写入注册表中的“Software \ Microsoft \ Windows \ CurrentVersion \ Run”。我很乐意让你们在代码上给我你的意见。有没有更有效的方法来写入注册表?Python注册表持久性
import sys, base64, os, socket, subprocess
from _winreg import *
def autorun(tempdir, fileName, run):
# Copy executable to %TEMP%:
os.system('copy %s %s'%(fileName, tempdir))
# Queries Windows registry for the autorun key value
# Stores the key values in runkey array
key = OpenKey(HKEY_LOCAL_MACHINE, run)
runkey =[]
try:
i = 0
while True:
subkey = EnumValue(key, i)
runkey.append(subkey[0])
i += 1
except WindowsError:
pass
# If the autorun key "helloworld" isn't set this will set the key:
if 'helloworld' not in runkey:
try:
key= OpenKey(HKEY_LOCAL_MACHINE, run,0,KEY_ALL_ACCESS)
SetValueEx(key ,'helloworld',0,REG_SZ,r"%TEMP%\hello.exe")
key.Close()
except WindowsError:
pass
def hello():
print "hello world"
def main():
tempdir = '%TEMP%'
fileName = sys.argv[0]
run = "Software\Microsoft\Windows\CurrentVersion\Run"
autorun(tempdir, fileName, run)
hello()
if __name__ == "__main__":
main()
感谢您的反馈意见。我实际上通过将HKEY_LOCAL_MACHINE更改为HKEY_CURRENT_USER来解决问题。但我有一个新问题。由于某种原因,它不会自动启动。 – holograms
没关系我得到它的工作。谢谢 – holograms