473,657 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Working with the Windows Registry

Hi everybody. I'm trying to write a script that'll change desktop
wallpaper every time its run. Heres what I've gotten so far:

#random wallpaper changer!
import _winreg
from os import walk
from os.path import exists
from random import randint

#first grab a registry handle.
handle = _winreg.OpenKey (_winreg.HKEY_C URRENT_USER,'Co ntrol Panel
\Desktop',_winr eg.KEY_SET_VALU E)

def GenerateListOfW allpapers():
targetDir = 'C:\Documents and Settings\Enrico Jr\My Documents\Jr
\'s Wallpapers'
fileNames = []
filePaths = []
if exists(targetDi r):
#proceed to make the list of files
for x,y,z in walk(targetDir) :
for name in z:
fileNames.appen d(name)
for item in fileNames:
filePaths.appen d(targetDir + '\\' + item)
return filePaths

def RandomlySelectW allpaper(filePa ths):
index = randint(0,len(f ilePaths)-1)
RandomlySelecte dWallpaper = filePaths[index]
return RandomlySelecte dWallpaper #it should be a string...

#now to edit the wallpaper registry key
newWallpaper = RandomlySelectW allpaper(Genera teListOfWallpap ers())
print "Registry Handle Created."
print "Random wallpaper selected."
_winreg.SetValu eEx(handle,'Con vertedWallpaper ',
0,_winreg.REG_S Z,newWallpaper)
print "New wallpaper value set."

The problem is, every time I run it, I get an "Access Denied" error
when it tries to execute
_winreg.SetValu eEx(), even though i've opened the key with the
KEY_SET_VALUE mask like it said in the help docs. Could there be
another problem or a better way to do this?
Jun 27 '08 #1
5 2508
On Jun 25, 9:48 pm, teh_sAbEr <teh.sa...@gmai l.comwrote:
Hi everybody. I'm trying to write a script that'll change desktop
wallpaper every time its run. Heres what I've gotten so far:

#random wallpaper changer!
import _winreg
from os import walk
from os.path import exists
from random import randint

#first grab a registry handle.
handle = _winreg.OpenKey (_winreg.HKEY_C URRENT_USER,'Co ntrol Panel
\Desktop',_winr eg.KEY_SET_VALU E)

def GenerateListOfW allpapers():
targetDir = 'C:\Documents and Settings\Enrico Jr\My Documents\Jr
\'s Wallpapers'
fileNames = []
filePaths = []
if exists(targetDi r):
#proceed to make the list of files
for x,y,z in walk(targetDir) :
for name in z:
fileNames.appen d(name)
for item in fileNames:
filePaths.appen d(targetDir + '\\' + item)
return filePaths

def RandomlySelectW allpaper(filePa ths):
index = randint(0,len(f ilePaths)-1)
RandomlySelecte dWallpaper = filePaths[index]
return RandomlySelecte dWallpaper #it should be a string...

#now to edit the wallpaper registry key
newWallpaper = RandomlySelectW allpaper(Genera teListOfWallpap ers())
print "Registry Handle Created."
print "Random wallpaper selected."
_winreg.SetValu eEx(handle,'Con vertedWallpaper ',
0,_winreg.REG_S Z,newWallpaper)
print "New wallpaper value set."

The problem is, every time I run it, I get an "Access Denied" error
when it tries to execute
_winreg.SetValu eEx(), even though i've opened the key with the
KEY_SET_VALUE mask like it said in the help docs. Could there be
another problem or a better way to do this?
Note the line

#first grab a registry handle.
handle = _winreg.OpenKey (_winreg.HKEY_C URRENT_USER, 'Control Panel
\Desktop', _winreg.KEY_SET _VALUE)

OpenKey() takes four arguments: (1) The Registry key handle or one of
the predefined constants, (2) the string containing the subkey to
open, (3) the 'res' (which I don't know what is :), and the 'sam',
which is the access mask (KEY_SET_VALUE, in this case). You are only
passing three arguments, so the access mask is going to the 'res'
argument instead of the 'sam' argument. Pass instead 0 as the res:
handle = _winreg.OpenKey (_winreg.HKEY_C URRENT_USER,
'Control Panel\Desktop',
0,
_winreg.KEY_SET _VALUE)

Regards,
Sebastian
Jun 27 '08 #2
teh_sAbEr wrote:
Hi everybody. I'm trying to write a script that'll change desktop
wallpaper every time its run. Heres what I've gotten so far:

#random wallpaper changer!
import _winreg
from os import walk
from os.path import exists
from random import randint

#first grab a registry handle.
handle = _winreg.OpenKey (_winreg.HKEY_C URRENT_USER,'Co ntrol Panel
\Desktop',_winr eg.KEY_SET_VALU E)

def GenerateListOfW allpapers():
targetDir = 'C:\Documents and Settings\Enrico Jr\My Documents\Jr
\'s Wallpapers'
fileNames = []
filePaths = []
if exists(targetDi r):
#proceed to make the list of files
for x,y,z in walk(targetDir) :
for name in z:
fileNames.appen d(name)
for item in fileNames:
filePaths.appen d(targetDir + '\\' + item)
return filePaths

def RandomlySelectW allpaper(filePa ths):
index = randint(0,len(f ilePaths)-1)
RandomlySelecte dWallpaper = filePaths[index]
return RandomlySelecte dWallpaper #it should be a string...

#now to edit the wallpaper registry key
newWallpaper = RandomlySelectW allpaper(Genera teListOfWallpap ers())
print "Registry Handle Created."
print "Random wallpaper selected."
_winreg.SetValu eEx(handle,'Con vertedWallpaper ',
0,_winreg.REG_S Z,newWallpaper)
print "New wallpaper value set."

The problem is, every time I run it, I get an "Access Denied" error
when it tries to execute
_winreg.SetValu eEx(), even though i've opened the key with the
KEY_SET_VALUE mask like it said in the help docs. Could there be
another problem or a better way to do this?
Common error. You have to open the key so that it can be written as follows:

reg = _winreg.HKEY_CU RRENT_USER
key = r'Control Panel\Desktop'
handle = _winreg.OpenKey (reg, key, 0, _winreg.KEY_WRI TE)
Note: be careful with backslashes (\) in non-raw strings they will be
interpreted as escaped sequences. You were lucky because \D doesn't
represent anything escaped. You should either use r's\gg\gg' or use double
backslashes 's\\gg\\gg'.

-Larry
Jun 27 '08 #3
On Jun 25, 10:48*pm, teh_sAbEr <teh.sa...@gmai l.comwrote:
Hi everybody. I'm trying to write a script that'll change desktop
wallpaper every time its run. Heres what I've gotten so far:

#random wallpaper changer!
import _winreg
from os import walk
from os.path import exists
from random import randint

#first grab a registry handle.
handle = _winreg.OpenKey (_winreg.HKEY_C URRENT_USER,'Co ntrol Panel
\Desktop',_winr eg.KEY_SET_VALU E)
You're missing the third parameter to OpenKey. Try:
handle = _winreg.OpenKey (_winreg.HKEY_C URRENT_USER,
'Control Panel\Desktop', _0, winreg.KEY_SET_ VALUE)
The problem is, every time I run it, I get an "Access Denied" error
when it tries to execute
_winreg.SetValu eEx(), even though i've opened the key with the
KEY_SET_VALUE mask like it said in the help docs. Could there be
another problem or a better way to do this?
Jun 27 '08 #4
Great! It works properly now but I have one more question, would
anyone know how to get the changes to take effect immediately? Like
some sort of Python way to force the desktop to reload? AFAIK the only
way that'll happen is if I use the Display Properties dialog box. The
Registry value is changed properly, its just I don't think the changes
will take effect until I restart.
Jun 27 '08 #5
teh_sAbEr <te*******@gmai l.comwrote:
>Hi everybody. I'm trying to write a script that'll change desktop
wallpaper every time its run. Heres what I've gotten so far:

#random wallpaper changer!
import _winreg
from os import walk
from os.path import exists
from random import randint

#first grab a registry handle.
handle = _winreg.OpenKey (_winreg.HKEY_C URRENT_USER,'Co ntrol Panel
\Desktop',_win reg.KEY_SET_VAL UE)

def GenerateListOfW allpapers():
targetDir = 'C:\Documents and Settings\Enrico Jr\My Documents\Jr
\'s Wallpapers'
You are fortunate that your name is not "Tim" or "Ian" or "Nathan", because
this would not have worked as you have written it.

You either need to double the backslashes:
... 'C:\\Documents and Settings\\Enric o...'
or use forward slashes:
... 'C:/Documents and Settings/Enrico...'
or use the "r" modifier:
... r'C:\Documents and Settings\Enrico ...'

However, as a general practice, it's probably better to get the special
directories from the environment:
targetDir = os.environ['USERPROFILE'] + '\\My Documents\\Jr\' s
Wallpapers'

Remember that it's not called "Documents and Settings" on Vista...
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jun 28 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
2313
by: Paul Rubin | last post by:
As what must be penance for something or other, I'm needing to release a Python app for use under Windows XP. Please be gentle with me since I'm a Un*x weenie and the only thing I've had much practice with under Windows is rebooting it. My app contains three different programs (say alice.py, bob.py, and carol.py) that need to be independently launchable, and a dozen or so other .py files that get imported into those first three. What...
0
1279
by: glenn | last post by:
I am trying to write to the registry and the objects that are suppose to be handling this function are not working. Here is what is happening. I am issuing the following commands: RegistryKey reg; reg = Registry.CurrentUser.CreateSubKey("Software\\SofTek Software\\SSILicense\\Settings"); reg = Registry.CurrentUser.OpenSubKey("Software\\mykey\\mysubkey\\Settings", true);
4
1855
by: Richard | last post by:
All, I have coded an Outlook automation Addin in C# and .NET. I created the project using the Extensibility wizard. The Addin installs and runs Ok on my machine. However I am unable to succsefully install the Addin on other machines. The install runs without error, it does unpack necessary files, and it does create many Addin related registry keys - but Outlook does not seem to be
5
12931
by: Dhilip Kumar | last post by:
Hi All, I'm writing a Windows Service app using C#. I need to read some configuration settings before the service starts up. These settings will be used by the service in its operation. Question is, which is the best way to store & retrieve the settings? I'm thinking of storing it in the registry in HKLM\Software\ServiceName and access it using the Registry class in the "public ServiceName()" method. I'm instructing the service...
3
1505
by: vighnesh | last post by:
Hi All I have to develop an application, which has to scan the windows registry and fix the bugs if any. Please let me know whether it is possible to develop that application in VB.NET/C#.NET? or has to go by some other means. I know how to interact with Registry in VB.NET/C#.NET.But I think it is not sufficient to deal
3
9688
by: Benny Raymond | last post by:
I get the following error message when trying to use the Excel Interop on my wife's machine however I don't get it on my own - we have the same version of Office installed - what could be the cause? ************** Exception Text ************** System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Office.Interop.Excel, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' or one of its dependencies....
4
3573
by: JohnB111 | last post by:
Hi Environment: Windows XPPRO SP2 IIS 5.1 AVG Anti Virus (Not script Blocking) The following code used to work perfectly on my local development machine and still does on my web server.
1
5561
by: chitra g | last post by:
Hi, I tried all the options below but did not work. Your suggestions please.
0
9205
AmberJain
by: AmberJain | last post by:
Windows Autorun FAQs: Description NOTE- If you are unfamiliar with the concept of autoruns, then read "Windows Autorun FAQs: Overview". Que-1: How can I safely remove or edit the autorun settings on my PC? Ans: I recommend that you backup all of your important data before trying anything mentioned in this article. When a person is tweaking with autoruns, one has to rely on 'trial and error' methods and so there is always the possibility...
0
8395
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8826
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7330
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4155
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1615
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.