Click here to Skip to main content
Click here to Skip to main content
Alternative Tip/Trick

Tagged as

Read Registry using C#

, 8 Dec 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
You can try the function below to read values from the Registry:myRegistry.Read("MY_KEY");where: input: the key name (string)output: the value of the key (string)using System;using Microsoft.Win32; public string Read(string KeyName){ // Opening the registry key ...

You can try the function below to read values from the Registry:

myRegistry.Read("MY_KEY");

where:


  • input: the key name (string)
  • output: the value of the key (string)
using System;
using Microsoft.Win32;  
public string Read(string KeyName)
{
    // Opening the registry key
      RegistryKey rk = baseRegistryKey ;
    // Open a subKey as read-only

    RegistryKey sk1 = rk.OpenSubKey(subKey);
    // If the RegistrySubKey doesn't exist -> (null)

    if ( sk1 == null )
    {
        return null;
    }
    else
    {
        try 
        {
       // If the RegistryKey exists I get its value or null is returned.        
         return (string)sk1.GetValue(KeyName.ToUpper());
        }
        catch (Exception e)
        {
            ShowErrorMessage(e, "Reading registry " + KeyName.ToUpper());
            return null;
        }
    }
}

Note: if MY_KEY doesn't exist, the Read function will return null.

Find some more functions to perform write, delete a single key, delete a subkey, and many more from this link: Read, write and delete from registry with C#[^]. In addition, have a look there-Reading and Writing to the Registry][^] to get detailed lessons on how to read and write to the Registry.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

RaviRanjankr
Software Developer
India India
Microsoft Student Partner | CS Student | MCTS | CP MVP | Crazy Learner | Dreamer
 
An Indian, who loves his country, believes in freedom, He is an enthusiast Techie and crazy learner. He is passionate about Technologies and social media. He holds bachelor degree of CS in Information Technology and now pursuing Master degree in Computer Application.
 
He always excited and keen Interested in learning and sharing knowledge. He loves to write blog, learn new things, listen music, taking arts and Playing Games..
 
He keep himself on the desk of his imagination, hanging around with some inceptions.
Follow on   Twitter   Google+

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web01 | 2.8.141022.2 | Last Updated 8 Dec 2011
Article Copyright 2011 by RaviRanjankr
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid