VisualBuilder
  Home > Csharp > Tutorials > Working with Registry in C sharp - C# Tutorial
Tell a friend
Link to us
Total Members
      Members: 84606
     
Sitemap Forum Chat
Home
C# Tutorial Home
1 . Introduction to C sharp
2 . Control Statement: Selection Statement in C sharp
3 . Control Statements – Loops Statement
4 . Methods
5 . Namespaces
6 . Introduction to Classes:
7 . Inheritance:
8 . Polymorphism
9 . Properties
10 . Indexers
11 . Structs
12 . Interfaces
13 . Delegates:
14 . Exception Handling:
15 . Attributes
16 . Enums
17 . Encapsulation
18 . Parameter Passing in C sharp
19 . Method Overloading
20 . Database Interaction Using C sharp
21 . Operator Overloading in C sharp -1
22 . Operator Overloading in C sharp -2
23 . Operator Overloading in C sharp -2
24 . Sockets
25 . DNS [Domain Name System]
26 . Working with Files
27 . Generating Help File in C sharp
28 . Code Access Security
29 . Multi-Threading
30 . Globalization and Localization -1
31 . Working with Registry in C sharp
32 . Globalization and Localization -2
33 . Windows Service
34 . Web Service
35 . Consuming Web Services
36 . Creating Proxy Object of Web Service
37 . Creating an XML document
38 . Reading XML document in C sharp
39 . Using XMLWriter class to Write XML document in C sharp
40 . Assembly Information : Getting Permission set of the assembly
41 . Creating your own Permission Set
42 . Using C sharp Socket
 
Csharp Group Home
Csharp Discussion (7)
Csharp Members (2250)
Csharp Resources
Csharp Source Code (35)
Csharp Articles (0)
Csharp Blogs
Csharp Jobs
Csharp Components (5)
Csharp Books
Csharp Websites (25)
Csharp News (17)
Csharp Q & A (4)
- Csharp Ask Question
- Csharp Questions
- Csharp Unanswered Questions
 
GROUPS
.NET
ASP.NET
.NET
C#
ASP
Visual Basic
Java
Java
JSP
EJB
Other
Delphi
C++
Ajax
UML
JavaScript
PHP
Web Design
Web Hosting
SQL Server
Oracle
Project Management
More Groups

 
LEARNING CENTER
TUTORIALS
.NET
.NET Tutorial
ASP Tutorial
ASP.NET Database Tutorial
ASP.NET Development Tips
ASP.Net Security,Internationalisation And Deployment
ASP.NET Server Controls Tips
ASP.NET Tutorial
C Sharp Tutorial
Web Development
Flex Tutorial
HTML Tutorial
Learn AJAX Tutorial
PHP Tutorial
Software Development
Database Tutorial
SQL Tutorial
UML Tutorial
Java
Ant Tutorial
EJB 3 Tutorial
Hibernate Tutorial
Java Tutorial
Java Web Component Tutorial
Java XML Tutorial
JDBC Tutorial
JDK1.5 Tutorial
JSF Tutorial
JSP And J2EE Design Tutorial
JSP Tutorial
Spring Tutorial
Struts Tutorial

RESOURCES
Q & A (432 )
Source Code (3217 )
Articles (11 )
Components (1589 )
News (880 )
Websites (1207 )

SUBMISSIONS
Submit Article
Submit Website
Submit News
Submit Source Code
Submit Component

COMMUNITY
Members Directory
Discussion Forum
Chat

SITE
About Us
Sitemap
Search
Contact Us
Link To Us
Feedback
Tell a Friend
Partners
Advertise


Csharp Tutorial
 Working with Registry in C sharp
  << Prev: Globalization and Localization -1 Next: Globalization and Localization -2 >>

Windows Registry is a central database for application configuration settings and other information required by the applications. The Windows Registry is a data repository that exists on each computer in Windows Operating systems. Both the system and application programs use this repository to store information needed at runtime. For example, the system stores file associations (the applications or command lines associated with each known file extension) in the Registry. Applications often use the Registry to store information such as users' option settings and data file pathnames.


 


Note:- If you've never open Windows registry, you can see it by running regedit from command line


 


.NET Framework Library provides two classes - Registry and RegistryKey to work with the windows registry. These classes are defined in Microsoft.Win32 namespace. So before using these classes, you need to add reference to this namespace.


 


Registry Class:


The Registry class contains members to provides access to registry keys. We can define registry keys in the following order.



  • CurrentUser - Stores information about user preferences.

  • LocalMachine - Stores configuration information for the local machine.

  • ClassesRoot - Stores information about types (and classes) and their properties.

  • Users - Stores information about the default user configuration.

  • PerformanceData - Stores performance information for software components.

  • CurrentConfig - Stores non-user-specific hardware information.

  • DynData - Stores dynamic data.


 


The Registry class members are described in the following table.


































ClassesRoot



Returns a RegistryKey type which provides access to HKEY_CLASSES_ROOT key.



LocalMachine



Returns a RegistryKey type which provides access to HKEY_LOCAL_MACHINE key.



CurrentConfig



Returns a RegistryKey type which provides access to HKEY_CURRENT_CONFIG key.



DynData



Returns a RegistryKey type which provides access to HKEY_DYN_DATA key.



CurrentUser



Returns a RegistryKey type which provides access to HKEY_CURRENT_USER key



PerformanceData



Returns a RegistryKey type which provides access to HKEY_PERFORMANCE_DATA key.



Users



Returns a RegistryKey type which provides access to HKEY_USERS key.



 


RegistryKey Class :


The RegistryKey class contains members to add, remove, replace, and read registry data. Some of its common methods and properties are defined in the following table.


 


Properties:


Name - Represents the name of the key.


SubKeyCount - Represents the count of subkeys at the base level, for theCurrent key.


ValueCount - Represents the count of values in the key.


 


Methods:














































GetValueNames



Retrieves an array of strings that contains all the value names associated with this key.



GetValue



Returns the specified value.



OpenSubKey



Opens a subkey.



GetSubKeyNames



Returns an array of strings that contains all the subkey names.



DeleteSubKeyTree



Deletes a subkey and any children.



DeleteSubKey



Deletes the specified subkey.



CreateSubKey



Creates a new subkey if not exists, otherwise opens an existing subkey.



SetValue



Sets the specified value.



DeleteValue



Deletes the specified value from a key.



Close



Closes the key.



 


 


Example:- Working with Windows Registry.


 


 


Form1.cs


 


using System;


using System.Collections.Generic;


using System.ComponentModel;


using System.Data;


using System.Drawing;


using System.Text;


using System.Windows.Forms;


using Microsoft.Win32;


namespace WindowsApplication1


{


  public partial class Form1 : Form


    {


    public Form1()


        {


    InitializeComponent();


        }


 


    private void button1_Click( object sender, EventArgs e)


        { //Adding a Key and Value in Registry


            RegistryKey key = Registry .LocalMachine.OpenSubKey( "Software" , true );


     //Create Key inside the software folder


            RegistryKey newkey = key.CreateSubKey( "NewTestKeyAdded" );


    //User Can also set the value for this Key


            newkey.SetValue( "NewTestKeyAdded" , "ValueForThisTestKey" );


    MessageBox.Show( "Key/Value is added in the Registry..." );


        }


 


    private void button2_Click( object sender, EventArgs e)


        { //Retrieving Data From Registry


            RegistryKey pRegKey = Registry .LocalMachine;


            pRegKey = pRegKey.OpenSubKey( "HARDWARE\\         DESCRIPTION\\System\\FloatingPointProcessor\\0" );


            Object valueOfKey = pRegKey.GetValue( "Identifier" );


      MessageBox.Show( "Key Value is :: " + valueOfKey);


        }


 


    private void button3_Click( object sender, EventArgs e)


        { //Deleting Key/Value from Registry


 


            RegistryKey delKeyValue = Registry .LocalMachine.OpenSubKey( "Software\\" );


delKeyValue.DeleteValue( "ValueForThisTestKey" );


     // Delete the key from the registry.


            RegistryKey delSubKey = Registry .LocalMachine.OpenSubKey( "Software" , true );


delSubKey.DeleteSubKey( "NewTestKeyAdded" );


      MessageBox.Show( "Deleting Key/Value in the Registry" );


        }


    }


}


 


Output:


 


 




 


 


Adding a Key/Value in the Registry


 




 


 


Retrieving Value from the Registry


 




 


 


  << Prev: Globalization and Localization -1 Next: Globalization and Localization -2 >>
Csharp Tutorial Home
Give feedback and win a prize.

 
   Printer Friendly
   Email to a friend
   Add to my Favourites    
  Download PDF version
   Report Bad Submissions
   Submit Feedback
 
  Delicious   Digg   Technorati   Blink   Furl   Reddit   Newsvine   Google Click each image to add
this page to each site.
 
 
Welcome Guest Signup
MEMBER'S PANEL
EMAIL
PASSWORD
Forgot your password?
New User? Click Here!
 
Resend Activation Email!
 
SEARCH
 
 
LINKS
MSN
Video Surveillance
Skype vs. sipcall
Gift to Pakistan
 
ADVERTISEMENT
 
PARTNER LIST

More
 
 
 

Home | Login | About Us | Contact Us | Privacy Policy | Advertising