C # read/write registry operations

Source: Internet
Author: User

Http://www.bhtsoft.com/Article.asp? ArticleID = 239

C # Read and Write operations on the registry. There are three methods in total. 1. Write the Registry. 2. Modify the Registry. 3. Check whether the key value exists.
Tested, this operation class is also applicable to the Development of. NET Compact Framework 2.0 and Windows Mobile Platforms.

First, introduce the namespace

Program code using Microsoft. Win32;

All code: program code using system;
Using system. Collections. Generic;
Using Microsoft. Win32;

Namespace register
{
Public class registeroperate
{

/**/
/// <Summary>
/// Write to the Registry
/// </Summary>
/// <Param name = "strname"> </param>
Public static void setregeditdata (string strname, string strvalue)
{
Try
{
Registrykey HKLM = registry. localmachine;
Registrykey software = HKLM. opensubkey ("software", true );
Registrykey aimdir = software. createsubkey ("mysoftware ");
Aimdir. setvalue (strname, strvalue );
}
Catch (exception ex)
{
Console. writeline (ex. Message );
}

}

/**/
/// <Summary>
/// Modify the registry key
/// </Summary>
/// <Param name = "strname"> </param>
/// <Param name = "strvalue"> </param>
Public static void modifyregeditdata (string strname, string strvalue)
{
Try
{
Registrykey HKLM = registry. localmachine;
Registrykey software = HKLM. opensubkey ("Software \ mysoftware", true );
Software. setvalue (strname, strvalue );
}
Catch (exception ex)
{
Console. writeline (ex. Message );
}
}

/**/
/// <Summary>
/// Determine whether the specified registry key exists
/// </Summary>
/// <Param name = "strname"> </param>
/// <Returns> </returns>
Public static bool isexist (string strname)
{
Try
{
Bool exit = false;
String [] subkeynames;
Registrykey hkml = registry. localmachine;
Registrykey software = hkml. opensubkey ("software", true );
Registrykey aimdir = software. opensubkey ("mysoftware", true );
Subkeynames = aimdir. getvaluenames ();
Foreach (string keyname in subkeynames)
{
If (keyname = strname)
{
Exit = true;
Return exit;
}
}
Return exit;
}
Catch (exception ex)
{
Console. writeline (ex. Message );
Return false;
}
}

}
}

Use this class:

Registeroperate. setregeditdata ("keyname", "keyValue ")
// Create the keyname key under HKEY_LOCAL_MACHINE \ SOFTWARE \ mysoftware

Registeroperate. modifyregeditdata ("keyname", "keyValue ")
// Modify the key value of the keyname under HKEY_LOCAL_MACHINE \ SOFTWARE \ mysoftware

Registeroperate. isexist ("keyname ")
// Determine whether the key keyname under HKEY_LOCAL_MACHINE \ SOFTWARE \ mysoftware exists

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.