C#. NET Modify the Registry

Source: Internet
Author: User
Tags directory create

C # modifies the registry and needs to reference the Microsoft.Win32 namespace

Using Microsoft.Win32; Declaration//quote RegistryKey REG; reg = Registry.classesroot;

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
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);
}
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
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
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;
}
The above is in the registry in the HKEY_LOCAL_MACHINE\Software directory in the XXX directory to determine whether the name of the registry key exists, this method in the deletion of the registry already exists, in the new registry key should be a corresponding judgment;

C#. NET Modify the 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.