MGZ NetArt

Software Development and Support

Home
Design Patterns
C# Tips
AsyncCallback
Asynchronous Form.ShowDia
BackgroundWorker
Connection String
CopyTo() vs Clone()
Convert Color To String
Dispose vs Finalize
Debug mode
Delete Duplicate in SQL
Drag&Drop for TreeView
Edit Full Path Property
Hiding And Overriding
Polimorphism
How to use Resources
Run Time DLL
Simple Threading
Singleton Connection
Save Win Form as JPG File
Save Object in Registry
Soap Serialization
Sort and CompareTo
Working With List<>
Examples & Videos
MRDS Examples
Resume
Contact Us
Save Object Properties in the Win Registry 
 

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);