using System;
using System.Windows.Forms;
using Microsoft.Win32;
using Northwoods.Go;
using System.Reflection;
namespace myDesigner
{
class UserSettingsStore
{
private string regPath;
private string objName;
private RegistryKey key;
public string RegictryPath
{
get { return regPath; }
}
public string ObjName
{
get { return objName; }
}
public UserSettingsStore(string registryPath, string formName)
{
this.regPath = registryPath;
this.objName = formName;
//creatae the Key if it does not exist
key = Registry.CurrentUser.CreateSubKey(registryPath + formName);
}
public void SaveSettings(Object mynode)
{
Type type = mynode.GetType();
FieldInfo[] fields = type.GetFields();
foreach (FieldInfo fld in fields)
{
key.SetValue(fld.Name, fld.GetValue(mynode));
}
}
public void ApplySettings(Object mynode)
{
Type type = mynode.GetType();
FieldInfo[] fields = type.GetFields();
foreach (FieldInfo fld in fields) {
fld.SetValue(mynode, key.GetValue(fld.Name));
}
}
public void SaveSettings(System.Windows.Forms.Form form)
{
key.SetValue("Height", form.Height);
key.SetValue("Width", form.Width);
key.SetValue("Left", form.Left);
key.SetValue("Top", form.Top);
}
public void ApplySettings(System.Windows.Forms.Form form)
{
//If form setting are not available, the current form setting are used instead.
form.Height = (int)key.GetValue("Height", form.Height);
form.Width = (int)key.GetValue("Width", form.Width);
form.Left = (int)key.GetValue("Left", form.Left);
form.Top = (int)key.GetValue("Top", form.Top);
}
}
}
How to Use
UserSettingsStore formSetting = new UserSettingsStore(@"Software\MyApp\", this.Name);
formSetting.SaveSettings(this);