The Settings class is written in C# and provides an easy way to store and load application settings/options in an ini-file like structure. The class uses an XML file as its underlying database but hides all the XML details from the user. All data are written/read with a simple interface of functions of the following form:
DataType ReadDataType(string section, string name, DataType defaultValue); void WriteDataType(string section, string name, DataType value);
Like ini-files the Settings class stores settings in sections which contain name-value pairs. The following example code shows the basic use:
//read settings Settings s = new Settings(“c:\\myprogram\\settings.xml”); string lastDir = s.ReadString(“Data1”, “LastDir”, “c:\\”); DateTime lastUsage = s.ReadDateTime(“Data1”, “LastUsage”, DateTime.Now); //write settings Settings s = new Settings(“c:\\myprogram\\settings.xml”); s.Writestring(“Data1”, “LastDir”, lastDir); s.WriteDateTime(“Data1”, “LastUsage”, DateTime.Now); s.Save();
This would result in the following XML file:
<Settings> <Data1> <LastDir value=”c:\\programs\\” /> <LastUsage value=”11/03/2003” /> </Data1> </Settings>
Note: since the file format is XML the section and name strings must follow the XML syntax rules. For example section and name strings must not contain spaces or begin with a number!
The zip file contains the C# source code and a release built assembly. Either add the code to your project and recompile it or set up a reference to the assembly file.
// If the file does not exist it is created Settings(string fileName, string rootName) // Uses "Settings" as default rootName Settings(string fileName) void Save() void ClearSection(string section) void RemoveSection(string section) // All reading functions catch format conversion exceptions. // If an exception is caught or the value does not exist the defaultValue is returned bool ReadBool(string section, string name, bool defaultValue) DateTime ReadDateTime(string section, string name, DateTime defaultValue) double ReadDouble(string section, string name, double defaultValue) float ReadFloat(string section, string name, float defaultValue) int ReadInt(string section, string name, int defaultValue) long ReadLong(string section, string name, long defaultValue) short ReadShort(string section, string name, short defaultValue) string ReadString(string section, string name, string defaultValue) uint ReadUInt(string section, string name, uint defaultValue) ulong ReadULong(string section, string name, ulong defaultValue) ushort ReadUShort(string section, string name, ushort defaultValue) void WriteBool(string section, string name, bool value) void WriteDateTime(string section, string name, DateTime value) void WriteDouble(string section, string name, double value) void WriteFloat(string section, string name, float value) void WriteInt(string section, string name, int value) void WriteLong(string section, string name, long value) void WriteShort(string section, string name, short value) void WriteString(string section, string name, string value) void WriteUInt(string section, string name, uint value) void WriteULong(string section, string name, ulong value) void WriteUShort(string section, string name, ushort value)
This software is supplied on an as-is basis. The author offers no warranty of its fitness for any purpose whatsoever, and accepts no liability whatsoever for any loss or damage incurred by its use. This software is not a supported product. The author accepts no commitment or liability to address any problems that may be encountered in using it; however, comments, suggestions and bug reports are always welcome!
For questions about this class visit http://www.heelana.com/Dotnet/Settings/Settings.html or email me at mail@heelana.com
Copyright 2003 Nicolas Reinschmidt