您的位置:首页 > 编程语言 > Python开发

python操作windows注册表

2012-02-28 14:48 447 查看
## file2autorun.py

import sys

import win32api

import win32con

import os

#regedit

def addfile2autorun(path):

runpath = "Software\Microsoft\Windows\CurrentVersion\Run"

hKey = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER, runpath, 0, win32con.KEY_SET_VALUE)

path = os.path.abspath(path)

if False == os.path.isfile(path):

return False

(filepath, filename) = os.path.split(path)

print path

print filename

#添加数据项

win32api.RegSetValueEx(hKey, filename, 0, win32con.REG_SZ, path)

win32api.RegCloseKey(hKey)

return True

def delfile2autorun(path):

runpath = "Software\Microsoft\Windows\CurrentVersion\Run"

hKey = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER, runpath, 0, win32con.KEY_SET_VALUE)

path = os.path.abspath(path)

if False == os.path.isfile(path):

return False

(filepath, filename) = os.path.split(path)

print path

print filename

#删除数据项

win32api.RegSetValueEx(hKey, filename, 0, win32con.REG_SZ, "")

win32api.RegCloseKey(hKey)

return True

path = 'Foda.exe'

if addfile2autorun(path):

print "added %s to autorun" % (path,)

else:

print "fail add %s to autorun!!!" % (path, )
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: