# Accessing windows registry using C#



## garv84 (Sep 10, 2008)

i am developing a C# application where i need to save the user input given by the user in a dialog prompt in the registry.then show the output say the font style and color of the text which i have selected in the form should be saved in the registry even when i close the application and open it again.can neone pls help me wth the C# code snippet.thanks.


----------



## RCuber (Sep 10, 2008)

^^ Do you want to use only registery? 
If you want to just store some application data like the ones you have said, you can use App.config file to use it. It is much simpler to store settings in app.config rather than in registery.

edit: Please mention the visual studio version you are using (2003 or 2005)

edit: Please mention the visual studio version you are using (2003 or 2005)


----------



## garv84 (Sep 11, 2008)

@charan

yes i wan to use the rgistry only.

i want to save the color and font dialog sttings on a text in the registry and when i reopen the application once again ..i want to retreive the same from the reg.


----------



## QwertyManiac (Sep 11, 2008)

Application config files would do the same. Why registry specifically?


----------



## RCuber (Sep 11, 2008)

Ok here you.....
 I have created two functions for you. 

Use ReadKey for reading a value from registery and SetKey to set the value of a registery. I havent done much testing with various key types but this should give you the kickstart needed.


```
[COLOR="Blue"]Private Sub[/COLOR] Button1_Click([COLOR="#0000ff"]ByVal[/COLOR] sender [COLOR="#0000ff"]As[/COLOR] System.Object, [COLOR="#0000ff"]ByVal[/COLOR] e [COLOR="#0000ff"]As[/COLOR] System.EventArgs) [COLOR="#0000ff"]Handles[/COLOR] Button1.Click
        SetKey([COLOR="Red"]"Software\TestApp"[/COLOR],[COLOR="#ff0000"] "Color"[/COLOR], [COLOR="#ff0000"]"Color.Red"[/COLOR])
        SetKey([COLOR="#ff0000"]"Software\TestApp"[/COLOR], [COLOR="#ff0000"]"Font"[/COLOR], [COLOR="#ff0000"]"Times New Roman"[/COLOR])
        MessageBox.Show(ReadKey([COLOR="#ff0000"]"Software\TestApp"[/COLOR], [COLOR="#ff0000"]"Font"[/COLOR]))
        MessageBox.Show(ReadKey([COLOR="#ff0000"]"Software\TestApp"[/COLOR], [COLOR="#ff0000"]"Color"[/COLOR]))
[COLOR="Blue"]End Sub[/COLOR]

    [COLOR="#0000ff"]Public Shared Function[/COLOR] ReadKey([COLOR="#0000ff"]ByVal[/COLOR] Key [COLOR="#0000ff"]As[/COLOR] [COLOR="#0000ff"]String[/COLOR], [COLOR="Blue"]ByVal[/COLOR] SubKey [COLOR="#0000ff"]As String[/COLOR]) [COLOR="#0000ff"]As String[/COLOR]
        Dim regKey As Microsoft.Win32.RegistryKey
        Dim ver As String = String.Empty
        [COLOR="#0000ff"]Try[/COLOR]
            regKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(Key)
            ver = regKey.GetValue(SubKey)
            regKey.Close()
        [COLOR="#0000ff"]Catch [/COLOR]ex [COLOR="#0000ff"]As[/COLOR] Exception
            [COLOR="#0000ff"]Return False[/COLOR]
      [COLOR="#0000ff"]  End Try[/COLOR]
        Return ver
    [COLOR="#0000ff"]End Function[/COLOR]

    [COLOR="#0000ff"]Public Shared Function[/COLOR] SetKey([COLOR="#0000ff"]ByVal[/COLOR] Key [COLOR="#0000ff"]As String[/COLOR], [COLOR="#0000ff"]ByVal [/COLOR]SubKey [COLOR="#0000ff"]As String[/COLOR], [COLOR="#0000ff"]ByVal[/COLOR] Value [COLOR="#0000ff"]As String[/COLOR]) [COLOR="#0000ff"]As Boolean[/COLOR]
        Dim regKey As Microsoft.Win32.RegistryKey
       [COLOR="Blue"] Try[/COLOR]
            regKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(Key, True)
            regKey.SetValue(SubKey, Value)
            regKey.Close()
           [COLOR="Blue"] Return True[/COLOR]
       [COLOR="#0000ff"] Catch[/COLOR] ex As Exception
           [COLOR="#0000ff"] Return False[/COLOR]
       [COLOR="#0000ff"] End Try[/COLOR]
    [COLOR="#0000ff"] End Function[/COLOR]
```


----------



## ManishSinha (Sep 11, 2008)

garv84 said:


> i am developing a C# application where i need to save the user input given by the user in a dialog prompt in the registry.then show the output say the font style and color of the text which i have selected in the form should be saved in the registry even when i close the application and open it again.can neone pls help me wth the C# code snippet.thanks.


Found this on a site

```
// Attempt to open the key
RegistryKey key = Registry.CurrentUser.OpenSubKey( "Software\\Play\\WindowPos" );

// If the return value is null, the key doesn't exist
if ( key == null ) 
{
    // The key doesn't exist; create it / open it
    key = Registry.CurrentUser.CreateSubKey( "Software\\Play\\WindowPos" );
}

// Attempt to retrieve the value X; if null is returned, the value
// doesn't exist in the registry.
if ( key.GetValue( "X" ) != null ) 
{
    // The value exists; move the form to the coordinates stored in the
    // registry.
    Location = new Point( (int)key.GetValue( "X" ), (int)key.GetValue( "Y" ) );
}
```



Charan said:


> Ok here you.....
> I have created two functions for you.
> 
> Use ReadKey for reading a value from registery and SetKey to set the value of a registery. I havent done much testing with various key types but this should give you the kickstart needed.
> ...


He asked for a C# code and not a VB code


----------



## garv84 (Sep 11, 2008)

One more thing i would like to seek u guys help.

i want to watermark a text in a "TEXT BOX",NOT ON AN IMAGE.
can neone pls help me with the C# snippet.thanx a ton.


----------



## RCuber (Sep 11, 2008)

ManishSinha said:


> He asked for a C# code and not a VB code


Woops  

neway here is the C# Code. 


```
private void Button1_Click(object sender, System.EventArgs e)
{
    SetKey("Software\\TestApp", "Color", "Color.Red");
    SetKey("Software\\TestApp", "Font", "Times New Roman");
    MessageBox.Show(ReadKey("Software\\TestApp", "Font"));
    MessageBox.Show(ReadKey("Software\\TestApp", "Color"));
}
public static string ReadKey(string Key, string SubKey)
{
    Microsoft.Win32.RegistryKey regKey;
    string ver = string.Empty;
    try {
        regKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(Key);
        ver = regKey.GetValue(SubKey);
        regKey.Close();
    }
    catch (Exception ex) {
        return false;
    }
    return ver;
}
public static bool SetKey(string Key, string SubKey, string Value)
{
    Microsoft.Win32.RegistryKey regKey;
    try {
        regKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(Key, true);
        regKey.SetValue(SubKey, Value);
        regKey.Close();
        return true;
    }
    catch (Exception ex) {
        return false;
    }
}
```



garv84 said:


> One more thing i would like to seek u guys help.
> i want to watermark a text in a "TEXT BOX",NOT ON AN IMAGE.


You mean like the foreground of the textbox must have a image ?


----------



## ruturaj3 (Sep 12, 2008)

Are u talking about Cue textbox. I hav used it in my project. 
Cue textbox are textbox in which some text appears when it doesn't focus.
For example, u will se cue textbox in vista. or in opera, in the address bar 'enter web address here'. U can download this control.


----------

