win7下python禁止开机自启动

win7下python禁止开机自启动


      今日学习了下python,正好最近正烦一些视频播放器一打开就将启动项加入到注册表,导致开机巨卡。所以参考网上资料,写了个python脚本,删除这些恼人的开机自启动项。

      主要的想法就是引入win32api和wincon,操作注册表,要安装python相应版本的winall包http://starship.python.net/crew/mhammond/downloads/
,我安装的python2.6.6,将HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run下的自启动项放入disableList.ini里,比如:

#config of disable items that not auto run at windows startup.
#check the auto run items under regedit.exe of HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
#and add them under the list, save and exit, we will disable them auto run.
Funshion   
QyKernel  
Thunder
BaofengPlatform

       然后python读取配置文件,删除对应的注册表项,代码如下:

</pre><pre name="code" class="python">#filename:disableAutoRun.py
#disable all things that auto run at the windows startup.
import win32api
import win32con


#read config file of disable auto run app list.
cnfList = []
def readCnf(filename):
    fp = open(filename, 'r')

    for disItem in fp.readlines():
                    
        if not len(disItem) or disItem.startswith('#') or disItem.startswith('\n'):
            continue
            
        # delete the space ' ' at the begin or end of the string    
        disItem = disItem.strip()    
        
        #print '', disItem
        cnfList.append(disItem)
    fp.close()
    print "disable auto run list:\n", cnfList      

#find disableItem under regedit, delete all in the config files.
def delAllDisableKeys(disableItem, subKey, valueName):
    global key
    try:   
        key = win32api.RegOpenKey(disableItem, subKey, 0, win32con.KEY_ALL_ACCESS)
        
        #print key, value
        #value = win32api.RegQueryValueEx(key, valueName)
        #print key, value
                        
        #now delete all valueName that we don't want startup.
        win32api.RegDeleteValue(key, valueName)
        print 'successfully disable the ', valueName, 'auto run item.'
    except IOError:
        print 'io error.'
    except IndentationError:
        print 'IndentationError error.'
    except:
        print 'fail to disable the ', valueName, ', maybe it not auto run.'
    finally:
        #print 'clost the item:', valueName  
          
        #close the key.
        win32api.RegCloseKey(key)
    

def pause():
    print '\n\npress any to exit...'
    temps = raw_input()
    

#do the regedit that disable them.
#check app in the dir.
currentUserKey='Software\Microsoft\Windows\CurrentVersion\Run'

#check app in the dir.
currentLocalKey='Software\Microsoft\Windows\CurrentVersion\Run'

#main()
if __name__ == '__main__':
    readCnf('.\\disableList.ini')
    
    print 'now disable the auto run items:'
    print '\n\ndisable auto run items under HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run:'
    for valueName in cnfList:
        delAllDisableKeys(win32con.HKEY_CURRENT_USER, currentUserKey, valueName)

    print '\n\ndisable auto run items under HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run:'
    for valueName in cnfList:
        delAllDisableKeys(win32con.HKEY_LOCAL_MACHINE, currentLocalKey, valueName)

    pause()



需要使用管理员运行,不然HKEY_LOCAL_MACHINE下的项不能打开。


参考:http://www.cnblogs.com/xiaowuyi/articles/2433161.html



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值