C # operate the Registry)

Source: Internet
Author: User

Http://www.cnblogs.com/whitetiger/archive/2008/04/09/1144219.html

 

 

 

1. Read the registry value of the specified name
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
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 );
}
In the registry, create a new XXX directory under the HKEY_LOCAL_MACHINE \ SOFTWARE Directory and create a registry entry named "tovalue" under the directory;

3. Delete the registry key specified in the registry.
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 );
}
}
In the Registry, the XXX directory under the HKEY_LOCAL_MACHINE \ SOFTWARE Directory deletes the name registry key;

4. Determine whether the specified registry key exists
Private bool IsRegeditExit (string name)
{
Bool _ exit = false;
Try
{
String [] subkeyNames;
RegistryKey hkml = Registry. LocalMachine;
RegistryKey software = hkml. OpenSubKey ("SOFTWARE", true );
RegistryKey aimdir = software. OpenSubKey ("Weather", true );
SubkeyNames = aimdir. GetValueNames ();
Foreach (string keyName in subkeyNames)
{
If (keyName = name)
{
_ Exit = true;
Return _ exit;
}
}
}
Catch
{}
Return _ exit;
}

In the Registry, the XXX directory under the HKEY_LOCAL_MACHINE \ SOFTWARE Directory determines whether the registry name is name or not. This method already exists when you delete the registry, when creating a registry entry, the corresponding judgment should also be made;

Example:

using Microsoft.Win32;
    using System.Diagnostics;
    private void Access_Registry()
    {
// Create a new key under HKEY_LOCAL_MACHINE \ Software and name it MCBInc
            RegistryKey key = Registry.LocalMachine.OpenSubKey("Software", true);
// Add a subkey
            RegistryKey newkey = key.CreateSubKey("MCBInc");
   
// Set the value of this subkey
            newkey.SetValue("MCBInc", "NET Developer");
         
// Obtain data from other places in the Registry
         
// Find your CPU
            RegistryKey pRegKey = Registry.LocalMachine;
            pRegKey = pRegKey.OpenSubKey("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0");
            Object val = pRegKey.GetValue("VendorIdentifier");
            Debug.WriteLine("The central processor of this machine is:"+ val);
// Delete the key value
            RegistryKey delKey = Registry.LocalMachine.OpenSubKey("Software", true);
            delKey.DeleteSubKey("MCBInc");
    }

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.