C# : How to store and delete value in REGISTRY for your Application

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CyberSoftHari
    Recognized Expert Contributor
    • Sep 2007
    • 488

    C# : How to store and delete value in REGISTRY for your Application

    We have to use Micrpsoft.Win32 functions.
    [code=cpp]using Microsoft.Win32 ;[/code]
    Then we have to use Registry class to do all our registry operations. Below will explain the members of Registry.
    Caution: Think twice before you do any thing in your registry because it may affect other applications, even windows operating system.

    How to CREATE a Registry Key
    Before creating a key we have to check the key (KeyName)should not available in creating registry path.
    Open a sub key with two parameters Type string of SubKeyName and Type bool of writeable. Then Create a Key with keyword SetValue and there two parameter Type string of KeyName and Type object of KeyValue.

    [code=cpp]
    if (Registry.Local Machine.OpenSub Key("SOFTWARE", false).GetValue (“KeyName”) == null)
    Registry.LocalM achine.OpenSubK ey("SOFTWARE ", true).SetValue( “KeyName”, "MyKeyValue ");
    [/code]

    How to DELETE a Registry Key
    Before deleting a key we have to check the key (KeyName) should available in deleting registry path.
    Open a sub key with two parameters Type string of SubKeyName and Type bool of writeable. Then Delete a Key with keyword DeleteValue and there parameter Type string of KeyName .

    [code=cpp]
    if (Registry.Local Machine.OpenSub Key("SOFTWARE", false).GetValue ((“KeyName”) != null)
    Registry.LocalM achine.OpenSubK ey("SOFTWARE", true).DeleteVal ue((“KeyName”);
    [/code]

    How to Check Sub Key in Registry
    [code=cpp]
    string[] strSubKeyNames = Registry.LocalM achine.OpenSubK ey("SOFTWARE"). GetSubKeyNames( );
    foreach (string strSubKeyName in strSubKeyNames)
    if (strSubKeyName == "MySoftware ")
    {
    //here you can write you Create subkey or delete subkey code
    return;
    }
    [/code]

    How to CREATE a Registry Sub Key
    Before Creating a sub key we have to check the sub key (MySoftware) should not available in creating registry path.
    Open a sub key with two parameters Type string of SubKeyName and Type bool of writeable. Then Create a Sub Key with keyword CreateSubKey and there parameter Type string of SubKeyName and type of RegistryKeyPerm issionCheck.

    [code=cpp]
    Registry.LocalM achine.OpenSubK ey("SOFTWARE",t rue).CreateSubK ey("MySoftware" , RegistryKeyPerm issionCheck.Def ault);
    [/code]
    We cal also code like below
    [code=cpp]
    Registry.LocalM achine.CreateSu bKey("SOFTWARE\ \MySoftware", RegistryKeyPerm issionCheck.Def ault);
    [/code]

    How to DELETE a Registry Sub Key
    Before Deleting a sub key we have to check the sub key (MySoftware) should available in deleting registry path.
    Open a sub key with two parameters Type string of SubKeyName and Type bool of writeable. Then Delete a Sub Key with keyword DeleteSubKey and there parameter Type string of SubKeyName.
    [code=cpp]
    Registry.LocalM achine.OpenSubK ey("SOFTWARE", true).DeleteSub Key("SOFTWARE\\ MySoftware");
    [/code]
    (unable to upload details)
Working...