Lindsey Bieda
Caffeine Powered Automaton

Mucking About in the Windows Registry for Fun and Profit

regeditThe Windows Registry contains settings and configuration information for computers running Microsoft Windows. It's stored as a hierarchy which resembles organization similar to folder structure. However, instead of having folders they are called keys. If you run "regedit" on any windows machine you will be presented a window that allows you to view the registry keys. Under 'Computer' generally there will be displayed five root keys. The root key we will be looking at is 'HKEY_CURRENT_USER' which stores information about the currently logged in user.

In C# to start we want to make sure the following is specified at the top of our code:
using Microsoft.Win32;

Using the registry key class we can open and close keys, get and set the value of keys, or delete and create new keys. To open HKEY_CURRENT_USER we simply do the following:
RegistryKey key = Registry.Users;

From there we can access any of the subkeys under HKEY_CURRENT_USER, for example if you wanted to access Control PanelDesktop in order to view the current user’s desktop settings so we could see what the user’s current wallpaper was.
// The true here means that we open it as writable
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true);
String wallpaper = key.GetValue("Wallpaper").ToString();
key.Close();
Console.WriteLine(wallpaper);

Notice that we need to pass the key value to a ToString method in order to get a string as a result because by default it will return an Object. Also when we are done with our key we should always remember to close it. Setting the value is just as simple if we wanted to change someone’s wallpaper to AdorableCuteKitties.jpg the code would simply be:
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true);
// note that the path to the wallpaper should be an absolute path here
key.SetValue("Wallpaper", "AdorableCuteKitties.jpg");
key.Close();

Creation and deletion is just as simple. By using the CreateSubKey() and DeleteValue() methods. It’s important to note that if you are deleting a key with child subkeys that DeleteSubKeyTree() should be used.

Toggle Screensaver Shortcut Using this method to modify user information I created a simple program in C# that would toggle the current user's screensaver enabled and disabled for when I watch TV or movies on my machine. It was made to work on Windows 7, which is why there are a few things a bit odd about the code. The ScreenSaveActive key in Windows 7 will always contain a value of 1 no matter what the state of the screensaver. So to get around that the key that stores what screensaver to use is first checked to see if it is active and to disable it the value from that key is temporarily stored in a dummy key (named Dummy) and the screensaver key is deleted. When it is re-enabled the screensaver key is restored based on what data is kept in the Dummy key.

The source and executable for the screensaver toggler can be downloaded: here.
The executable alone can be downloaded: here.
name :
email :