私は、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
私はそれが働くように気にしません。 thanks – holograms