C # Operations Registry

Source: Internet
Author: User
Tags directory create

The Registry of the Windows operating system contains a lot of information about how the computer is running, and opening the registry we can see that the registry is organized in a directory-like tree structure, where the second-level directory contains five predefined primary keys: Hkey_classes_root,hkey_ Current_user,hkey_local_machine,hkey_users,hkey_current_config.

   let's explain the effects of these 5 classes separately.

HKEY_CLASSES_ROOT the primary key contains the file's extension and the application's associated information, as well as the window shell and OLE information used to store the registry. The subkeys under the primary key determine how the class files and their icons are displayed in Windows, and the primary key is mapped from hkey_lccal_machine\software\classes.

HKEY_CURRENT_USER The primary key contains information such as the User's window information, desktop settings, and other current users.

The HKEY_LOCAL_MACHINE primary key contains installation and configuration information for computer software and hardware that can be used by all users
HKEY_USERS the primary key records the current user's settings information, each time the user logs into the system, the primary key will be generated under the same name as the user login subkey, the child key saved the current user's desktop settings, background bitmap, shortcut keys, fonts and other information. The general application does not directly access the primary key, but rather accesses it through the primary keys HKEY_CURRENT_USER.

Hkey_current_config This primary key holds the configuration information for the computer's current hardware, which can be changed depending on the type of network currently connected or the hardware-driven software installation.

   C #Editing of the registry is also supported. NET Framework provides two classes in the Microsoft.Win32 namespace tooperating the Registration form: Registry and RegistryKey. These two classes are sealed classes that are not allowed to be inherited. Let's introduce each of the two classes below.

The Registry class provides 7 public static domains representing 7 basic primary keys (two of which are not in the XP system, In this is not introduced) are: Registry.classesroot,registry.currentuser,registry.localmachine,registry.users,registry.currentconfig. How many keys do they correspond to? I think you'll know when you see it.

The RegistryKey class provides methods for registry operations. It's important to note thatoperating the Registration formSystem permissions must be met, or an error will be thrown.

   Here are a few ways to operate the registry

The method prototypes for creating sub-keys are:

Public RegistryKey CreateSubKey (string sunbkey);



The parameter Sunbkey represents the name or path name of the subkey to be created. Create successfully returns the child key that was created, otherwise null is returned.

The method prototype for opening the child keys is:

Public RegistryKey OpenSubKey (string name);
Public RegistryKey OpenSubKey (string name,bool writable);


The parameter name indicates the subkey name to open or its path name, and the parameter writable indicates whether the subkey opened is allowed to be modified, and the subkey opened by the first method is read-only. The Microsoft.Win32 class also provides us with another way to open the registry on a remote computer, using the following method:

public static RegistryKey OpenRemoteBaseKey (registryhive hkey,string machinename);


The method prototypes for deleting subkeys are:

public void DeleteKey (string subkey);


This method is used to delete the specified primary key. If the subkey to be deleted also contains the primary key then the delete fails and returns an exception, if you want to completely delete the subkey under the extreme directory of the subkey can be used method Deletesubkeytree, the method prototype is as follows:

public void Deletekeytree (string subkey);


The method prototype for reading the key values is as follows:

public Object GetValue (string name);
public Object GetValue (string Name,object defaultvalue);


The parameter name represents the name of the key, the return type is an object type, or null if the specified key does not exist. If you fail and you do not want the value returned to be null, you can specify the parameter defaultvalue, specifying the parameter to return the value specified by the parameter in the case of a read failure.

The method for setting the key value is as follows:

public Object SetValue (string name,object value);



A class that operates the registry

This morning wrote a simple class to manipulate the registry.
Now post out let everybody help me to see, have what question please contact me.

Using System;
Using Microsoft.Win32;

Namespace Powercomponent
{
<summary>
Operating the Registration form
</summary>
public class Register
{

<summary>
Instance constructors
</summary>
Public Register ()
{
//
TODO: Add constructor logic here
//
}

#region Public methods

  ///<summary>
  ///writes to the registry, modifies the value of the specified item if the specified item already exists
  ///</summary
  ///<param name= "KeyType" > Registry base key Enumeration </param>
  ///<param name= "Key" > registry key, excluding base item </param>
  ///<param name= "name" > Value name </param>
  /// <param name= "Values" > Value </param>
  ///<returns> Returns a Boolean value that specifies whether the operation succeeded </returns>
  public bool SetValue (PowerComponent.Register.keyType KeyType, string key, string name, string values)
& nbsp; {
   try
   {
    registrykey rk = ( RegistryKey) Getregistrykey (keytype);

RegistryKey Rkt = rk. CreateSubKey (key);

if (Rkt! = null)
Rkt. SetValue (name, values);
Else
{
Throw (The new Exception ("The item to write does not exist");
}

return true;
}
Catch
{
return false;
}

}


<summary>
Read the Registration Form
</summary>
<param name= "KeyType" > Registry base Key Enumeration </param>
<param name= "key" > registry key, excluding base items </param>
<param name= "name" > Value name </param>
<returns> return String </returns>
public string GetValue (PowerComponent.Register.keyType keyType, string key, string name)
{
Try
{
RegistryKey RK = (RegistryKey) getregistrykey (KeyType);

RegistryKey Rkt = rk. OpenSubKey (key);

if (Rkt! = null)
Return RKT. GetValue (name). ToString ();
Else
Throw (New Exception ("Cannot find specified item"));
}
catch (Exception ex)
{
Return ex. Message;
}

}


<summary>
To delete a value from the registry
</summary>
<param name= "KeyType" > Registry base Key Enumeration </param>
<param name= "key" > registry key name, excluding base items </param>
<param name= "name" > Value name </param>
<returns> returns a Boolean value that specifies whether the operation was successful </returns>
public bool DeleteValue (PowerComponent.Register.keyType KeyType, string key, string name)
{
Try
{
RegistryKey RK = (RegistryKey) getregistrykey (KeyType);

RegistryKey Rkt = rk. OpenSubKey (key,true);

if (Rkt! = null)
Rkt. DeleteValue (name,true);
Else
Throw (New Exception ("Cannot find specified item"));

return true;

}
Catch
{
return false;
}
}


<summary>
Delete the specified item in the registry
</summary>
<param name= "KeyType" > Registry base Key Enumeration </param>
<param name= "key" > Keys in the registry, excluding base items </param>
<returns> returns a Boolean value that specifies whether the operation was successful </returns>
public bool DeleteSubKey (PowerComponent.Register.keyType KeyType, string key)
{
Try
{
RegistryKey RK = (RegistryKey) getregistrykey (KeyType);

Rk. Deletesubkeytree (key);

return true;

}
Catch
{
return false;
}
}


<summary>
Determines whether the specified item exists
</summary>
<param name= "KeyType" > Base Item Enumeration </param>
<param name= "Key" > Specified item string </param>
<returns> returns a Boolean value indicating whether the specified item exists </returns>
public bool Isexist (PowerComponent.Register.keyType KeyType, string key)
{
RegistryKey RK = (RegistryKey) getregistrykey (KeyType);

if (RK. OpenSubKey (key) = = NULL)
return false;
Else
return true;
}


<summary>
Retrieves all values associated with the specified item
</summary>
<param name= "KeyType" > Base Item Enumeration </param>
<param name= "Key" > Specified item string </param>
<returns> returns a string array of all values associated with the specified item </returns>
Public string[] GetValues (PowerComponent.Register.keyType keyType, string key)
{
RegistryKey RK = (RegistryKey) getregistrykey (KeyType);

RegistryKey Rkt = rk. OpenSubKey (key);

if (Rkt! = null)
{

string[] names = Rkt. GetValueNames ();

if (names. Length = = 0)
return names;
Else
{
String[] values = new string[names. Length];

int i = 0;

foreach (string name in Names)
{
Values[i] = Rkt. GetValue (name). ToString ();

i++;
}

return values;

}
}
Else
{
Throw (New Exception ("The specified item does not exist"));
}

}

#endregion

#region Private Methods

<summary>
Returns the RegistryKey object
</summary>
<param name= "KeyType" > Registry base Key Enumeration </param>
<returns></returns>
Private Object Getregistrykey (PowerComponent.Register.keyType keyType)
{
RegistryKey RK = null;

Switch (keytype)
{
Case Keytype.hkey_class_root:
RK = Registry.classesroot;
Break
Case Keytype.hkey_current_user:
RK = Registry.currentuser;
Break
Case Keytype.hkey_local_machine:
RK = Registry.localmachine;
Break
Case Keytype.hkey_users:
RK = registry.users;
Break
Case Keytype.hkey_current_config:
RK = Registry.currentconfig;
Break
}

return RK;
}

#endregion

#region Enumeration
<summary>
Registry Base Key Enumeration
</summary>
public enum Keytype:int
{
<summary>
Registry Base Key HKEY_CLASSES_ROOT
</summary>
Hkey_class_root,
<summary>
Registry base Key HKEY_CURRENT_USER
</summary>
HKEY_CURRENT_USER,
<summary>
Registry Base Key HKEY_LOCAL_MACHINE
</summary>
HKEY_LOCAL_MACHINE,
<summary>
Registry Base Key HKEY_USERS
</summary>
HKEY_USERS,
<summary>
Registry Base Key Hkey_current_config
</summary>
Hkey_current_config
}
#endregion

}
}


The following four cases from ' read ' ' write ' ' delete ' ' judgment ' to implement simple operation on the registry
1. Read the value of the registry with the specified name

Program code
private string Getregistdata (string name)
{
String Registdata;
RegistryKey hkml = registry.localmachine;
RegistryKey software = hkml. OpenSubKey ("Software", true);
RegistryKey Aimdir = software. OpenSubKey ("XXX", true);
Registdata = Aimdir. GetValue (name). ToString ();
return registdata;
}

The above is the registry value named name in the XXX directory under the HKEY_LOCAL_MACHINE\Software directory in the read registry;

2. Write data to the registry

Program code
private void Wtregedit (String name,string tovalue)
{
RegistryKey HKLM = Registry.localmachine;
RegistryKey software = HKLM. OpenSubKey ("Software", true);
RegistryKey Aimdir = software. CreateSubKey ("XXX");
Aimdir. SetValue (Name,tovalue);
}
The above is the new XXX directory under the HKEY_LOCAL_MACHINE\Software directory in the registry and in this directory create a registry key with the name value Tovalue;

3. Delete the registry key specified in the registry

Program code
private void Deleteregist (string name)
{
String[] Aimnames;
RegistryKey hkml = registry.localmachine;
RegistryKey software = hkml. OpenSubKey ("Software", true);
RegistryKey Aimdir = software. OpenSubKey ("XXX", true);
Aimnames = Aimdir. Getsubkeynames ();
foreach (String aimkey in Aimnames)
{
if (Aimkey = = name)
Aimdir. Deletesubkeytree (name);
}
}
The above is the name of the registry key that is removed from the HKEY_LOCAL_MACHINE\Software directory in the registry under the XXX directory;

4. Determine if the specified registry key exists

Program code
private bool Isregeditexit (string name)
{
BOOL _exit = false;
String[] Subkeynames;
RegistryKey hkml = registry.localmachine;
RegistryKey software = hkml. OpenSubKey ("Software", true);
RegistryKey Aimdir = software. OpenSubKey ("XXX", true);
Subkeynames = Aimdir. Getsubkeynames ();
foreach (String keyName in Subkeynames)
{
if (keyName = = name)
{
_exit = true;
return _exit;
}
}
return _exit;
}

C # Operations Registry

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.