#1
  1. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154

    Writing to the Registry


    Can any tell me how to write to the Registry in Python? I need to add my programs file name to HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\run.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Location
    Regensburg, Germany
    Posts
    147
    Rep Power
    16
    Python contains a module named "_winreg" which allows you to manipulate the windows registry. For details see the current python documentation. For a working example of how to use it have a look at e.g. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/146305.
  4. #3
  5. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Thanks. That link helped a lot. I'm still trying to get it to add my program to a place other than Software, but it's better than nothings, thanks again.I think I figured it out, I know have:
    Code:
    class WindowsRegistry:
    
        def __init__(self, company="Microsoft", project="Windows\CurrentVersion", write=1):
            """
            handle registry access
            """
            self.write = write
            self.company = company
            self.project = project
            self.keyname = "Software\\%s\\%s" % (self.company, self.project)
    
            try:
                self.key = wreg.OpenKey(wreg.HKEY_CURRENT_USER, self.keyname)
            except:
                if self.write:
                    self.key = wreg.CreateKey(wreg.HKEY_CURRENT_USER, self.keyname)
    
        def set(self, name, value):
            " set value in registry "
            if not self.write:
                raise Exception, "registry is read only"
            wreg.SetValue(self.key, name, wreg.REG_SZ,str(value))
    
        def pset(self, name, value):
            " set using pickle "
            self.set(name, pickle.dumps(value))
    
        def get(self, name):
            " get value out of registry "
            return wreg.QueryValue(self.key, name)
    
        def pget(self, name):
            " get using pickle "
            return pickle.loads(self.get(name))
    
        def close(self):
            " close the key finally "
            self.key.Close()
    
        def __del__(self):
            self.close()
    
    if __name__=="__main__":
        r = WindowsRegistry(project="Windows\CurrentVersion", write=1)
        r.set("run", "C:\Documents and Settings\Master\Desktop\UOChat.py")
        print 'Created!'
    This put my file in CurrentVersion\Run. This should work correctly. I'm just trying to change it's name from (Default) to PythonBot.

IMN logo majestic logo threadwatch logo seochat tools logo