Having come to C#/.Net from a Delphi background I thought the ease of
storing local options was a bit lacking so I have created a class that does
just the type of thing you want.
1. You need to take the code below and create Settings.cs
2. Then in your program create a new class descended from SettingsObjectBase
that has all the settings you need to store in it. You can even have
collections etc.
public class MyAppSettings : SettingsObjectBase
{
public string Name;
public string Address;
public List<MyStuff> MyList = new List<MyStuff>();
}
3. Then at the start of your program do this:
SettingsStore<MyAppSettings> MySettingsStore = new
SettingsStore<MyAppSettings>(SettingsStoreLocation .UserApplicationLevel)
4. It will auto load as soon as you decclare it (if it can). But after this
you can do SettingsStore.Load() and SettingsStore.Save()
5. Then all you need to do is work with MyAppSettings throughout your
program to access your settings.
Let me know if this class is useful to anyone.
Code for Settings.cs:
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Windows.Forms;
namespace YourCompanyName.General
{
/// <summary>
/// Where to place the settings store
/// </summary>
public enum SettingsStoreLocation
{
/// <summary>
/// In the file specified
/// </summary>
SpecifiedFile,
/// <summary>
/// In the user home directory in the product folder
/// </summary>
UserProductLevel,
/// <summary>
/// In the user home directory in the product/appname folder
/// </summary>
UserApplicationLevel,
/// <summary>
/// In the common home directory in the product folder
/// </summary>
AllUsersProductLevel,
/// <summary>
/// In the common home directory in the product/appname folder
/// </summary>
AllUsersApplicationLevel
}
/// <summary>
/// Generic class for storing settings in XML
/// </summary>
public class SettingsStore<T> where T : SettingsObjectBase, new()
{
#region Construction
/// <summary>
/// Constructor for standard storage location + part file name
/// </summary>
/// <param name="Location">The place to store the settings</param>
/// <param name="FileName">The last part of the file name excluding
the
/// path unless using SpecifiedFile in which case this is the
/// full file name</param>
public SettingsStore(SettingsStoreLocation Location, string
FileName)
{
SetupLocation(Location, FileName);
}
/// <summary>
/// Constructor for standard storage location
/// </summary>
/// <param name="Location">The place to store the settings</param>
public SettingsStore(SettingsStoreLocation Location)
{
SetupLocation(Location, string.Empty);
}
/// <summary>
/// Constructor for specified storage location with full file name
/// </summary>
/// <param name="FileName">The full filename for where to store the
/// settings</param>
public SettingsStore(string FileName)
{
SetupLocation(SettingsStoreLocation.SpecifiedFile, FileName);
}
/// <summary>
/// Constructor for a specified file location that you do not know
yet.
/// You must set the FileName propery before calling Save/Load.
/// </summary>
/// <param name="Location"></param>
public SettingsStore()
{
SetupLocation(SettingsStoreLocation.SpecifiedFile,
string.Empty);
}
// Setup where the file will be stored
void SetupLocation(SettingsStoreLocation Location, string FileName)
{
_Location = Location;
if (_Location != SettingsStoreLocation.SpecifiedFile)
if (FileName.Trim() == string.Empty)
FileName = "Settings.xml";
switch (_Location)
{
case SettingsStoreLocation.SpecifiedFile:
_FileName = FileName;
break;
case SettingsStoreLocation.UserProductLevel:
_FileName = Environment.GetFolderPath(
Environment.SpecialFolder.ApplicationData) +
Path.DirectorySeparatorChar +
Application.CompanyName +
Path.DirectorySeparatorChar +
_ProductName + Path.DirectorySeparatorChar +
FileName;
break;
case SettingsStoreLocation.UserApplicationLevel:
_FileName = Environment.GetFolderPath(
Environment.SpecialFolder.ApplicationData) +
Path.DirectorySeparatorChar +
Application.CompanyName +
Path.DirectorySeparatorChar +
_ProductName + Path.DirectorySeparatorChar +
Path.GetFileNameWithoutExtension(Application.Execu tablePath)
+
Path.DirectorySeparatorChar +
FileName;
break;
case SettingsStoreLocation.AllUsersProductLevel:
_FileName = Environment.GetFolderPath(
Environment.SpecialFolder.CommonApplicationData) +
Path.DirectorySeparatorChar +
Application.CompanyName +
Path.DirectorySeparatorChar +
_ProductName + Path.DirectorySeparatorChar +
FileName;
break;
case SettingsStoreLocation.AllUsersApplicationLevel:
_FileName = Environment.GetFolderPath(
Environment.SpecialFolder.CommonApplicationData) +
Path.DirectorySeparatorChar +
Application.CompanyName +
Path.DirectorySeparatorChar +
_ProductName + Path.DirectorySeparatorChar +
Path.GetFileNameWithoutExtension(Application.Execu tablePath)
+
Path.DirectorySeparatorChar +
FileName;
break;
}
if ((_FileName != string.Empty) && (File.Exists(_FileName)))
Load();
else
Reset();
}
#endregion
#region Settings Access
// Internal store of settings object
T _Settings;
/// <summary>
/// The settings object holding the actual settings
/// </summary>
public T Settings
{
get
{
return _Settings;
}
}
#endregion
#region Storage
SettingsStoreLocation _Location;
/// <summary>
/// Where the settings will be stored
/// </summary>
public SettingsStoreLocation Location
{
get
{
return _Location;
}
}
string _FileName;
/// <summary>
/// The filename where the settings will be stored
/// </summary>
public string FileName
{
get
{
return _FileName;
}
set
{
_FileName = value;
_Location = SettingsStoreLocation.SpecifiedFile;
}
}
string _ProductName = Application.ProductName;
/// <summary>
/// This defaults to Application.ProductName but you can change it
/// e.g. if you want to create a shared product name to hold
settings
/// shared between a group of products
/// </summary>
public string ProductName
{
get
{
return _ProductName;
}
set
{
_ProductName = value;
}
}
/// <summary>
/// Save the settings to disk
/// </summary>
public void Save()
{
if (FileName.Trim() == string.Empty)
throw new Exception("No FileName has been specified");
Directory.CreateDirectory(Path.GetDirectoryName(_F ileName));
using (StreamWriter Writer = new StreamWriter(_FileName))
{
XmlSerializer Serializer = new XmlSerializer(typeof(T));
Serializer.Serialize(Writer, _Settings);
}
}
/// <summary>
/// Load the settings from disk. This happens automatically if you
/// specify a location when creating the object.
/// </summary>
public void Load()
{
if (FileName.Trim() == string.Empty)
throw new Exception("No FileName has been specified");
if (!File.Exists(_FileName))
throw new Exception("The file does not exist");
using (FileStream Reader = new FileStream(_FileName,
FileMode.Open))
{
XmlSerializer Serializer = new XmlSerializer(typeof(T));
_Settings = (T)Serializer.Deserialize(Reader);
}
}
/// <summary>
/// Reset the settings to their default state
/// </summary>
public void Reset()
{
_Settings = new T();
}
/// <summary>
/// Resets the settings to their default state and deletes the
storage file
/// </summary>
public void Delete()
{
if (File.Exists(_FileName))
File.Delete(_FileName);
Reset();
}
#endregion
}
/// <summary>
/// Base class for settings objects.
/// You must inherit your settings object from this.
/// </summary>
public abstract class SettingsObjectBase
{
}
}
--
Andrew Cutforth - AJC Software -
www.ajcsoft.com
The best folder synchronize and directory compare tool available.
AJC Active Backup instantly archives every file you edit giving you
unlimited undo and automatic revision control. Never lose your data again.