Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 10-09-2008, 04:29 PM   #1 (permalink)
Right Off the Assembly Line
 
Join Date: Jul 2007
Posts: 26
Exclamation Accessing windows registry using C#


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.
garv84 is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 10-09-2008, 04:31 PM   #2 (permalink)
Rubik's Uncle!!
 
Charan's Avatar
 
Join Date: Sep 2004
Location: ಬೆಂಗಳೂರು (Bengaluru)
Posts: 3,785
Default Re: Accessing windows registry using C#

^^ 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)
__________________
i5 2400 | DH67BL | G.Skill Ripjaw 4 GB | FSP SAGA II 500W | CM 430 Black Elite | MSI R6850 Cyclone PE/OC | XBox 360 Controller | 21.5" Samsung Sync Master 2233 | 4 Mbps @75GB FUP :)
Battlefield 3 Multiplayer Discussion | Battlefield 3 Low Latency Servers List

Last edited by Charan; 10-09-2008 at 04:34 PM. Reason: Automerged Doublepost
Charan is online now  
Old 11-09-2008, 10:40 AM   #3 (permalink)
Right Off the Assembly Line
 
Join Date: Jul 2007
Posts: 26
Default Re: Accessing windows registry using C#

@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.
garv84 is offline  
Old 11-09-2008, 05:13 PM   #4 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: Accessing windows registry using C#

Application config files would do the same. Why registry specifically?
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Old 11-09-2008, 05:25 PM   #5 (permalink)
Rubik's Uncle!!
 
Charan's Avatar
 
Join Date: Sep 2004
Location: ಬೆಂಗಳೂರು (Bengaluru)
Posts: 3,785
Default Re: Accessing windows registry using C#

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.

Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        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"))
End Sub

    Public Shared Function ReadKey(ByVal Key As String, ByVal SubKey As String) As String
        Dim regKey As Microsoft.Win32.RegistryKey
        Dim ver As String = String.Empty
        Try
            regKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(Key)
            ver = regKey.GetValue(SubKey)
            regKey.Close()
        Catch ex As Exception
            Return False
        End Try
        Return ver
    End Function

    Public Shared Function SetKey(ByVal Key As String, ByVal SubKey As String, ByVal Value As String) As Boolean
        Dim regKey As Microsoft.Win32.RegistryKey
        Try
            regKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(Key, True)
            regKey.SetValue(SubKey, Value)
            regKey.Close()
            Return True
        Catch ex As Exception
            Return False
        End Try
     End Function
__________________
i5 2400 | DH67BL | G.Skill Ripjaw 4 GB | FSP SAGA II 500W | CM 430 Black Elite | MSI R6850 Cyclone PE/OC | XBox 360 Controller | 21.5" Samsung Sync Master 2233 | 4 Mbps @75GB FUP :)
Battlefield 3 Multiplayer Discussion | Battlefield 3 Low Latency Servers List
Charan is online now  
Old 11-09-2008, 05:59 PM   #6 (permalink)
Addicted to FOSS
 
ManishSinha's Avatar
 
Join Date: Jan 2008
Location: Manipal
Posts: 32
Default Re: Accessing windows registry using C#

Quote:
Originally Posted by garv84 View Post
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
Code:
// 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" ) );
}
Quote:
Originally Posted by Charan View Post
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.

Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        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"))
End Sub

    Public Shared Function ReadKey(ByVal Key As String, ByVal SubKey As String) As String
        Dim regKey As Microsoft.Win32.RegistryKey
        Dim ver As String = String.Empty
        Try
            regKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(Key)
            ver = regKey.GetValue(SubKey)
            regKey.Close()
        Catch ex As Exception
            Return False
        End Try
        Return ver
    End Function

    Public Shared Function SetKey(ByVal Key As String, ByVal SubKey As String, ByVal Value As String) As Boolean
        Dim regKey As Microsoft.Win32.RegistryKey
        Try
            regKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(Key, True)
            regKey.SetValue(SubKey, Value)
            regKey.Close()
            Return True
        Catch ex As Exception
            Return False
        End Try
     End Function
He asked for a C# code and not a VB code

Last edited by ManishSinha; 11-09-2008 at 05:59 PM. Reason: Automerged Doublepost
ManishSinha is offline  
Old 11-09-2008, 06:06 PM   #7 (permalink)
Right Off the Assembly Line
 
Join Date: Jul 2007
Posts: 26
Default Re: Accessing windows registry using C#

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.
garv84 is offline  
Old 11-09-2008, 06:20 PM   #8 (permalink)
Rubik's Uncle!!
 
Charan's Avatar
 
Join Date: Sep 2004
Location: ಬೆಂಗಳೂರು (Bengaluru)
Posts: 3,785
Default Re: Accessing windows registry using C#

Quote:
Originally Posted by ManishSinha View Post
He asked for a C# code and not a VB code
Woops

neway here is the C# Code.

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;
    }
}
Quote:
Originally Posted by garv84 View Post
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 ?
__________________
i5 2400 | DH67BL | G.Skill Ripjaw 4 GB | FSP SAGA II 500W | CM 430 Black Elite | MSI R6850 Cyclone PE/OC | XBox 360 Controller | 21.5" Samsung Sync Master 2233 | 4 Mbps @75GB FUP :)
Battlefield 3 Multiplayer Discussion | Battlefield 3 Low Latency Servers List
Charan is online now  
Old 12-09-2008, 09:53 AM   #9 (permalink)
In The Zone
 
ruturaj3's Avatar
 
Join Date: Feb 2007
Location: Mumbai
Posts: 214
Default Re: Accessing windows registry using C#

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.
ruturaj3 is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Windows cannot find "rose.exe" error, when accessing Hard disk drives ! vishesh8 Software Q&A 14 11-01-2007 10:01 PM
Which Windows API fuiction create GUID for Windows Registry nandip Tutorials 1 31-10-2006 05:24 PM
Accessing windows partition from Fedora Core 4... plsoft Open Source 21 08-04-2006 02:46 PM
Tut 4 Windows Registry slugger Tutorials 3 16-01-2006 12:37 AM
Accessing Windows Files... deepak_vsoni Open Source 1 30-10-2005 01:44 PM

 
Latest Threads
- by clinton
- by soumya
- by Sujeet
- by clmlbx

Advertisement




All times are GMT +5.5. The time now is 11:12 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2