This blog is about the dotnet.all types of codes,news about dotnet including asp.net,vb.net,c# and know about new dotnet technology.programing in asp.net,vb.net,c#, ajax, AJAX tech support for .net and discuss the new technology in dotnet.ncluding asp.net,vb.net,c# and know about new dotnet technology.programing in asp.net,vb.net,c#, ajax, AJAX tech support for .net and discuss the new technology in dotnet.asp.net programming,dot net programming,dotnet programs,dotnet source code,source code.

Free Hosting

Free Hosting

Tuesday, November 11, 2008

how to manipulate windows registry|Registry Manipulation Basis

1. Basics and key elements

The Windows Registry is the place where most of the settings in Windows and its applications reside. For example, when a user finds the file TEST.TXT in Windows Explorer, it is automatically marked as a text file and Explorer shows the standard "Text file" icon. This behavior is a setting stored in the Registry. Same goes when trying to open that file: the Windows Registry stores the fact that Windows should use Notepad to open the file, and not Media Player, for instance.

The Windows Registry is divided into more subgroups, depending on its purpose:

HKEY_LOCAL_MACHINE - stores machine-specific settings, such as hardware, application settings that apply to all users, etc.

HKEY_CURRENT_USER - stores the current user's settings. It refers to the user that is logged on at the moment. If there are more users logged on, when one of them tries to access this key, it will point to its own settings, and not to the ones of another user

HKEY_CLASSES_ROOT - stores class-specific data. Each application or application element has its own UID (Unique IDentifier) that holds a certain place within this part of the registry. Generally, things like an Internet Explorer toolbar or a plug-in holds its data here, as well as all the known file extensions

HKEY_CURRENT_CONFIG - holds current configuration settings. The key is not very important, as it only holds session-specific data and few miscellaneous settings

HKEY_USERS - stores data about all users. Each user has a UUID (Unique User IDentifier). The HKEY_CURRENT_USER is actually a copy of one of the keys in here. Any modification will be lost if the user is logged on, because HKEY_USERS is automatically overwritten at logoff

The Registry is organized much like the file system in Windows. It has keys (which correspond to folders) and values (which correspond to files). Values can be of a few specific types: String, Binary, DWORD, Multi-string and Expandable String. Due to its limitations, however, C# will interpret the last two types as String types.

2. The perquisites

A C# application cannot access the registry at its own volition. Registry access cannot be granted to any application due to security reasons. It must ask for privileges in order to be able to access the registry.

Keep in mind that the registry is a windows-specific element. It will not be compatible with any other operating system. All classes and methods used to manipulate registry are stored within the "Microsoft.Win32" namespace. Also, keep in mind that registry manipulation is not done by the .NET Framework (or at least not yet implemented). That is why two assembly requests must be made (example):


using System.Security.Permissions;
using Microsoft.Win32;
assembly:SecurityPermissionAttribute(SecurityAction.RequestMinimum, Flags = SecurityPermissionFlag.UnmanagedCode)
assembly:RegistryPermissionAttribute(SecurityAction.RequestMinimum, All="HKEY_LOCAL_MACHINE")

Putting these in front of the namespace used to access registry will do the following:

- Request a permission to execute unmanaged code (the registry manipulation methods are outside the .NET Framework, therefore unmanaged code privilege is needed), signaling that it is a minimal requirement for that namespace or class to be able to execute its code, and inform that the program should fail if this is not fully satisfied
- Request a permission to access the HKEY_LOCAL_MACHINE base key and have access to all available actions (the "All" parameter), again signaling and informing like above

Of course, HKEY_LOCAL_MACHINE can be changed to any of the base classes above and the permission type can be changed from "All" to something else.

Next comes the actual manipulation phase.

3. The manipulation

The .NET Framework provides the "RegistryKey" class that allows registry manipulation.

It is impractical to open a base class manually, so the .NET Framework offers a way to open one automatically. for instance, let's presume we want to open a subclass of the HKEY_LOCAL_MACHINE located in "Software\Microsoft\Windows\CurrentVersion\Run" (the key that holds the programs that start up at boot-time). it can be done simply using the pre-defined method "Microsoft.Win32.Registry.LocalMachine.OpenSubKey()", that returns a RegistryKey? object. For example:


RegistryKey ka=Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", true);

The second parameter of the function is the "write-protect" parameter. If false (or if omitted), the registry key cannot be written.

Here are some examples of using the "RegistryKey" class:


ka.SetValue("GinaDLL", val); // After opening the registry key like specified, it sets the value GinaDLL to the one held in the "val" object; creates a new value if missing

ka.DeleteValue("GinaDLL", false); // After opening the registry key, it deletes the value "GinaDLL"; the second parameter states that if the value is missing, the function should not throw any exceptions

ka.Flush(); //After opening the key and doing some operations, it flushes all modifications to the Registry

ka.Close(); // After a key is opened, this flushes all modifications and then closes it

ka.CreateSubKey("MSGina"); // After a key is opened, this creates a sub-key

ka.DeleteSubKey("MSGina2"); // After a key is opened, this deletes a sub-key and all its contents

ka.DeleteSubKeyTree("ACertainSubKey"); // After opening a key, it deletes a specific sub-key and all its own sub-keys recursively

string[] alfa=ka.GetValueNames(); // Gets all the values currently in that key

That is all about basic registry manipulation. Do not forget to use the "Close" function, because otherwise, any modifications will be lost upon exit or there may be exceptions.

For further reference about C# registry manipulation, please consult MSDN at http://msdn.microsoft.com and for samples, please visit CodeProject? (if available) at http://www.codeproject.com or visit our Registry Manipulation section.

0 comments:

dotnet(.Net) Project Source code Downloads and Tutorials

Email Subscrption



Enter your email address:

Delivered by FeedBurner

Feedburner Count

Blog Archive

Unique Visitor

Design by araba-cı | MoneyGenerator Blogger Template by GosuBlogger