Click here to Skip to main content
15,880,469 members
Articles / Programming Languages / C#
Article

Saving Registry Settings

Rate me:
Please Sign up or sign in to vote.
3.96/5 (8 votes)
7 Nov 2006CPOL1 min read 62.9K   882   25   11
Saving Registry Settings in Visual C#

Application Settings

Summary

How to persist application settings in C# by saving them to the registry.

Overview

We can make several real world applications with Microsoft Visual C Sharp. An application can hold many settings. Application settings is a common description of an program. But there are no alternative methods in C# to handle application settings.

The System.Configuration.AppSettingsReader class allows to read its properties but it is read only in run time. In Visual Basic, there are two methods GetSetting and SaveSetting which can be used to save settings in registry. But it is not available in C#. This article can be useful for beginners to save application settings.

The Class

The code is very easy. There is only one class for handling settings. You can copy the code, add the code to your project and use it. The name of the class is RegistryHelper.

C#
public class RegistryHelper
{

}

The Code

C#
using System;
using Microsoft.Win32;
using System.Windows.Forms;
namespace BDNotePad.Modules
{
public class RegistryHelper
{
private static string FormRegKey(string sSect)
{
return sSect;
}
public static void SaveSetting(string Section, string Key,string Setting)
{
        
string text1 = FormRegKey(Section);
RegistryKey key1 =
    
Application.UserAppDataRegistry.CreateSubKey(text1);
if (key1 == null)
{
return;
}
try
{
key1.SetValue(Key, Setting);
}
catch (Exception exception1)
{
return;
}
finally
{
key1.Close();
}

}
public static string GetSetting(string Section, string Key, string Default )
{
if (Default == null)
{
Default = "";
}
string text2 = FormRegKey(Section);
RegistryKey key1 = Application.UserAppDataRegistry.OpenSubKey(text2);
if (key1 != null)
{
object obj1 = key1.GetValue(Key, Default);
key1.Close();
if (obj1 != null)
{
if (!(obj1 is string))
{
return null;
}
return (string) obj1;
}
return null;
}
return Default;
}
}
}

Using the Code

The code is very simple and easy to use. In the Load and Closed event of a form, you can use the code.

For example:

C#
protected override void OnLoad(EventArgs e)
{
    base.OnLoad (e);

    this.Width = Convert.ToInt32(RegistryHelper.GetSetting("Layout","Width",400));
}

protected override void OnClosed(EventArgs e)
{
    base.OnClosed (e);

    RegistryHelper.SaveSetting("Layout","Width",this.Width.ToString());
}

Conclusion

If you find this article useful or if you find any bugs, please let me know. Please send comments to mksamsks@yahoo.com.

May God keep you well.

History

  • 7th November, 2006: Initial post

About the Author

My name is Muhammed Kawser Ahmed. I am from Bangladesh. I am 15 years old and am learning C#. I study in grade 8. I like to share as much knowledge as possible with other people.

License

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


Written By
Bangladesh Bangladesh
I am a student of class 8. My country is in Bangladesh. A interest boy in programming. Age - 15 years old. Email - mksams@hotmail.com

Comments and Discussions

 
QuestionFormRegKey function Pin
Chuck Claunch9-Dec-13 7:49
Chuck Claunch9-Dec-13 7:49 
GeneralOnClosed and OnCloseing events don't get called. Pin
jamesdk827-Dec-10 5:34
jamesdk827-Dec-10 5:34 
QuestionCompare to VB6' GetSetting, SaveSetting Pin
Sandhya Yamarthi4-Sep-09 9:49
Sandhya Yamarthi4-Sep-09 9:49 
GeneralThank you! Pin
mycheck17-May-09 23:11
mycheck17-May-09 23:11 
GeneralGreat code. Thank you ! Pin
Roman Lerman11-Oct-07 8:04
Roman Lerman11-Oct-07 8:04 
Generalhi Pin
batlai28-Aug-07 23:22
batlai28-Aug-07 23:22 
GeneralGreat Code Pin
Fakher Halim24-Apr-07 7:16
Fakher Halim24-Apr-07 7:16 
GeneralThanks for the code Pin
berickson18-Nov-06 11:46
berickson18-Nov-06 11:46 
GeneralRe: Thanks for the code Pin
Mohammed Kawser12-Dec-06 4:55
Mohammed Kawser12-Dec-06 4:55 
Also thanks to you;

MKSAMS
QuestionWhy not use a xml-file Pin
André Ziegler7-Nov-06 11:18
André Ziegler7-Nov-06 11:18 
AnswerRe: Why not use a xml-file Pin
Mohammed Kawser12-Dec-06 4:58
Mohammed Kawser12-Dec-06 4:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.