 |
10-09-2008, 04:29 PM
|
#1 (permalink)
|
|
Right Off the Assembly Line
Join Date: Jul 2007
Posts: 26
|
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.
|
|
|
|
Advertisements. Register and be a member of the community to get rid of them.
|
|
Advertisement
|
|
10-09-2008, 04:31 PM
|
#2 (permalink)
|
|
Rubik's Uncle!!
Join Date: Sep 2004
Location: ಬೆಂಗಳೂರು (Bengaluru)
Posts: 3,785
|
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)
Last edited by Charan; 10-09-2008 at 04:34 PM.
Reason: Automerged Doublepost
|
|
|
11-09-2008, 10:40 AM
|
#3 (permalink)
|
|
Right Off the Assembly Line
Join Date: Jul 2007
Posts: 26
|
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.
|
|
|
11-09-2008, 05:13 PM
|
#4 (permalink)
|
|
Commander in Chief
Join Date: Jul 2005
Posts: 6,658
|
Re: Accessing windows registry using C#
Application config files would do the same. Why registry specifically?
__________________
Harsh J
www.harshj.com
|
|
|
11-09-2008, 05:25 PM
|
#5 (permalink)
|
|
Rubik's Uncle!!
Join Date: Sep 2004
Location: ಬೆಂಗಳೂರು (Bengaluru)
Posts: 3,785
|
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
|
|
|
11-09-2008, 05:59 PM
|
#6 (permalink)
|
|
Addicted to FOSS
Join Date: Jan 2008
Location: Manipal
Posts: 32
|
Re: Accessing windows registry using C#
Quote:
Originally Posted by garv84
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
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
|
|
|
11-09-2008, 06:06 PM
|
#7 (permalink)
|
|
Right Off the Assembly Line
Join Date: Jul 2007
Posts: 26
|
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.
|
|
|
11-09-2008, 06:20 PM
|
#8 (permalink)
|
|
Rubik's Uncle!!
Join Date: Sep 2004
Location: ಬೆಂಗಳೂರು (Bengaluru)
Posts: 3,785
|
Re: Accessing windows registry using C#
Quote:
Originally Posted by ManishSinha
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
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 ?
|
|
|
12-09-2008, 09:53 AM
|
#9 (permalink)
|
|
In The Zone
Join Date: Feb 2007
Location: Mumbai
Posts: 214
|
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.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|