Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,
I am new in C#.net I have a doubt in Window Registry.How to access Windows Registry?and what is the Purpose for using Windows Registry? In my Project i am using Window registry. I give an example and Please clear my doubt.My task is accessing only text file in my Project. When i am click a text file.I will show my Project otherwise error message will show.So I am using Window registry.is it right ? please suggest me.
Regards,
Narayanan
Posted

1 solution

Hi,
Windows registry keep program information how to start and where to start.
Aprat from that it stores some information related to that program


Here is a class that u can user to access registry


Imports Microsoft.Win32
Public Class Registries
    ' TODO : Complete registry class------completed
    ' Using the class
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'if just the keyname is passed to one of the constructors then the class will 
    ' return keyvalue through its "keyvalue" properties
    ' if both keyname and keyvalue is passed as arg to one of the overloaded constructor
    ' then the class will create a key if it does not exist and assign the key the value
    ' under the key hive localmachine\software\myApp
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#Region " Variables "
    Dim m_SubKey As String = "myApp" ' name of the key to be created under HKLM\Software---
    ' Will be name of the back office system
    Dim m_KeyValue As String ' will hold the value of the registry key passed as an arg to one of the constructor
#End Region
#Region " Properties "
    Public ReadOnly Property KeyValue() As String
        Get
            Return m_KeyValue
        End Get
    End Property
#End Region
#Region " Constructors "
    Public Sub New(ByVal keyname As String)
        ' contructor used to return the value of the key passed
        Call ReturnKeyValue(keyname)
    End Sub
    Public Sub New(ByVal KeyName As String, ByVal keyValue As String)
        ' input parameters 
        ' keyname : name of the key to be created under the software\focus
        Call Createkey(KeyName, keyValue)
    End Sub
    Public Sub New(ByVal SubKey As String, ByVal keyName As String, ByVal keyValue As String)
        m_SubKey = SubKey
        Call Createkey(keyName, keyValue)
    End Sub
#End Region
#Region " Methods "
    Private Sub Createkey(ByVal keyName As String, ByVal keyValue As String)
        Dim regKey As RegistryKey
        Dim iFoundKey As Int32 = -1
        Dim arrKeyNames() As String
        ' find whether the key Focus exists 
        ' if it does not exists then create one
        regKey = Registry.LocalMachine.OpenSubKey("Software", True)
        ' check for existence of the key 'myApp' under the key 'Software'
        arrKeyNames = regKey.GetSubKeyNames()
        iFoundKey = System.Array.IndexOf(arrKeyNames, "myApp")
        If iFoundKey = -1 Then
            Try
                regKey.CreateSubKey(m_SubKey) ' create the name of the software(Focus) as a sub key under HKLM\Software key
                regKey.Close()
            Catch ex As Exception
                RecordError.WriteErr(ex.Message)
                regKey.Close()
                Return
            End Try
        End If
        ' Do the same for the keyname
        regKey = Registry.LocalMachine.OpenSubKey("Software\" & m_SubKey, True)
        arrKeyNames = regKey.GetSubKeyNames
        iFoundKey = -1
        iFoundKey = System.Array.IndexOf(arrKeyNames, keyName)
        If iFoundKey = -1 Then ' key not found , create the key
            Try
                regKey.CreateSubKey(keyName) ' create the sub key under the Focus sub key
                regKey.SetValue(keyName, keyValue)
                regKey.Close()
                Return
            Catch ex As Exception
                RecordError.WriteErr(ex.Message)
            End Try
        End If
        Try
            regKey.SetValue(keyName, keyValue)
        Catch ex As Exception
        Finally
            regKey.Close()
        End Try
    End Sub
    Private Sub ReturnKeyValue(ByVal keyname As String)
        Try
            Dim regKey As RegistryKey
            regKey = Registry.LocalMachine.OpenSubKey("Software\myApp")
            m_KeyValue = regKey.GetValue(keyname)
        Catch ex As Exception
            m_KeyValue = ""
        End Try
        
    End Sub
#End Region
End Class



You can create registry key like

VB
Dim ReadRegistry As Registries
ReadRegistry = New Registries("LocalDatabase")


To access value
XML
Databasestring= ReadRegistry.KeyValue
 
Share this answer
 
Comments
Sandeep Mewara 2-May-11 5:33am    
My 5!

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900