15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

eading <strong>and</strong> Writing to the registry ❘ 809<br />

The most obvious difference is in how you open a registry key at a given location in the registry. The<br />

Registry class does not have any public constructor that you can use, nor does it have any methods that let<br />

you go directly to a key, given its name. Instead, you are expected to browse down to that key from the top<br />

of the relevant hive. If you want to instantiate a RegistryKey object, the only way is to start off with the<br />

appropriate static property of Registry, <strong>and</strong> work down from there. So, for example, if you want to read<br />

some data in the HKLM/Software/Microsoft key, you would get a reference to it like this:<br />

RegistryKey hklm = Registry.LocalMachine;<br />

RegistryKey hkSoftware = hklm.OpenSubKey("Software");<br />

RegistryKey hkMicrosoft = hkSoftware.OpenSubKey("Microsoft");<br />

A registry key accessed in this way will give you read-only access. If you want to write to the key (that<br />

includes writing to its values or creating or deleting direct children of it), you need to use another override<br />

to OpenSubKey, which takes a second parameter, of type bool, that indicates whether you want read-write<br />

access to the key. For example, if you want to be able to modify the Microsoft key (<strong>and</strong> assuming that you<br />

are a system administrator with permission to do this), you would write this:<br />

RegistryKey hklm = Registry.LocalMachine;<br />

RegistryKey hkSoftware = hklm.OpenSubKey("Software");<br />

RegistryKey hkMicrosoft = hkSoftware.OpenSubKey("Microsoft", true);<br />

Incidentally, because this key contains information used by Microsoft’s applications, in most cases you<br />

probably shouldn’t be modifying this particular key.<br />

The OpenSubKey() method is the one you call if you are expecting the key to be present. If the key isn’t<br />

there, it returns a null reference. If you want to create a key, you should use the CreateSubKey() method<br />

(which automatically gives you read-write access to the key through the reference returned):<br />

RegistryKey hklm = Registry.LocalMachine;<br />

RegistryKey hkSoftware = hklm.OpenSubKey("Software");<br />

RegistryKey hkMine = hkSoftware.CreateSubKey("MyOwnSoftware");<br />

The way that CreateSubKey() works is quite interesting. It creates the key if it does not already exist,<br />

but if it does exist, it quietly returns a RegistryKey instance that represents the existing key. The reason<br />

for the method behaving in this manner has to do with how you normally use the registry. The registry,<br />

overall, contains long-term data such as configuration information for Windows <strong>and</strong> for various<br />

applications. It is not very common, therefore, that you find yourself in a situation where you need to<br />

explicitly create a key.<br />

What is much more common is that your application needs to make sure that some data is present in the<br />

registry — in other words, create the relevant keys if they do not already exist, but do nothing if they<br />

do. CreateSubKey() fills that need perfectly. Unlike the situation with FileInfo.Open(), for example,<br />

there is no chance with CreateSubKey() of accidentally removing any data. If deleting registry keys is<br />

your intention, you will need to call the RegistryKey.DeleteSubKey() method. This makes sense given<br />

the importance of the registry to Windows. The last thing you want is to completely break Windows<br />

accidentally by deleting a couple of important keys while you are debugging your <strong>C#</strong> registry calls!<br />

After you have located the registry key you want to read or modify, you can use the SetValue() or<br />

GetValue() methods to set or get at the data in it. Both of these methods take a string giving the name of<br />

the value as a parameter, <strong>and</strong> SetValue() requires an additional object reference containing details of the<br />

value. Because the parameter is defined as an object reference, it can actually be a reference to any class you<br />

want. SetValue() will decide from the type of class actually supplied whether to set the value as a REG_SZ,<br />

REG_DWORD or REG_BINARY value. For example:<br />

RegistryKey hkMine = HkSoftware.CreateSubKey("MyOwnSoftware");<br />

hkMine.SetValue("MyStringValue", "Hello World");<br />

hkMine.SetValue("MyIntValue", 20);<br />

This code sets the key with two values: MyStringValue will be of type REG_SZ, <strong>and</strong> MyIntValue will be of<br />

type REG_DWORD. These are the only two types you will consider here <strong>and</strong> use in the example presented later.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!