C # how to operate the Registry

Source: Internet
Author: User

Using Microsoft. Win32;

1. Read the registry value of the specified name

Copy codeThe Code is as follows: 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

Copy codeThe Code is as follows: 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.

Copy codeThe Code is as follows: 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

Copy codeThe Code is as follows: 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;
}

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;

Copy codeThe Code is as follows: using System. Windows. Forms;
Using Microsoft. Win32;

Namespace RegeditManager
{
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}
// Create and write
Private void button#click (object sender, EventArgs e)
{
RegistryKey key = Registry. LocalMachine;
Try
{
RegistryKey software = key. CreateSubKey ("software \ LabManager ");
Software = key. OpenSubKey ("software \ LabManager", true );
Software. SetValue ("Address", @ "C: \ Program Files \ Laboratory Management System \ dbcom. xml ");
}
Catch (Exception ex)
{
MessageBox. Show (ex. ToString ());
}
Finally
{
Key. Close ();
}
}
// Read
Private void button2_Click (object sender, EventArgs e)
{
String info = string. Empty;
RegistryKey key = Registry. LocalMachine;
Try
{
Key = key. OpenSubKey ("software \ LabManager ");
If (IsRegeditKeyExit ("software \ LabManager", "Address "))
{
Info = key. GetValue ("Address"). ToString ();
MessageBox. Show ("the information in the registry is:" + info );
}
Else
{
MessageBox. Show ("the key value Address does not exist ;");
}
}
Catch (Exception ex)
{
MessageBox. Show (ex. ToString ());
}
Finally
{
Key. Close ();
}
}
// Delete
Private void button3_Click (object sender, EventArgs e)
{
RegistryKey key = Registry. LocalMachine;
Try
{
Key = key. OpenSubKey ("software \ LabManager", true );
If (IsRegeditKeyExit ("software \ LabManager", "Address "))
{
Key. DeleteValue ("Address ");
MessageBox. Show ("deleted successfully ");
}
Else
{
MessageBox. Show ("the key value Address does not exist ;");
}
}
Catch (Exception ex)
{
MessageBox. Show (ex. ToString ());
}
Finally
{
Key. Close ();
}
}

/// <Summary>
/// Determine whether the key value exists
/// </Summary>
/// <Param name = "RegistryStr"> registry key </param>
/// <Param name = "KeyStr"> key value </param>
/// <Returns> </returns>
Private bool IsRegeditKeyExit (string RegistryStr, string KeyStr)
{
String [] subkeyNames;

RegistryKey hkml = Registry. LocalMachine;

RegistryKey software = hkml. OpenSubKey (RegistryStr );

SubkeyNames = software. GetValueNames ();

Foreach (string keyName in subkeyNames)
{
If (keyName = KeyStr) // judge the key value name
{
Hkml. Close ();

Return true;
}
}
Hkml. Close ();

Return false;
}

/// <Summary>
/// Determine whether the registry key exists
/// </Summary>
/// <Param name = "RegistryName"> example: SOFTWARE </param>
/// <Param name = "ValueStr"> example: LabManager </param>
/// <Returns> </returns>
Private bool IsRegeditItemExist (string RegistryName, string ValueStr)
{
String [] subkeyNames;

RegistryKey hkml = Registry. LocalMachine;

RegistryKey software = hkml. OpenSubKey (RegistryName );

SubkeyNames = software. GetSubKeyNames ();

// Obtain the sequence of names of all sub-items under the item and pass it to the predetermined Array

Foreach (string keyName in subkeyNames) // traverses the entire array
{
If (keyName = ValueStr) // you can specify the subitem name.
{
Hkml. Close ();
Return true;
}
}
Hkml. Close ();
Return false;

}
}
}

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.