Settings.Load(); // Static method
And to access a setting
string listenIp = Settings.Instance.ListenerIp;
or
Settings settings = Settings.Instance;
string listenIp = settings.ListenerIp;
using System;
using System.IO;
using System.Xml;
using System.Drawing;
using System.Xml.Serialization;
using System.Diagnostics;
using EventLoggerLib;
namespace SettingsLib
{
public class Settings
{
#region Singleton
private static Settings instance = null;
public static Settings Instance
{
get
{
if(instance == null)
instance = new Settings();
return instance;
}
}
public Settings()
{
}
#endregion Singleton
public static void RestoreAllDefaults()
{
instance = new Settings();
}
//System
public bool EnableDataUpload = true;
public bool AutoStart = true;
public string ListenerIp = "192.168.101.53";
public string ServerIp = "192.168.101.10";
public bool ArchiveData = true;
public bool EnableDiagnostics = true;
// Specific Info
public string CompanyID = "WMT";
public string TruckID = "2416";
// Rfid
public bool RfidSerialConnect = true;
public int RfidComPort = 3;
public string RfidBaudRate = "57600";
public double RfidReadDelay = 0.1;
public string RfidIpAddress = "192.168.100.95";
public string RfidPort = "2101";
// Gps
public int GpsPort = 4;
public int GpsBaud = 4800;
// Scale
public bool UseReadTimeout = true;
public bool UseWriteTimeout = true;
public short ScaleReadTimeout = 500;
public short ScaleWriteTimeout = 500;
// Scale Port Settings
public int CurbScalePort = 1;
public int StreetScalePort = 2;
public string ScaleBaudRate = "9600";
public string ScaleDataBits = "8";
public string ScaleParity = "None";
public string ScaleStopBits = "1";
public string ScaleReadCommand = "(NL)";
#region Save / Load
public static void Save()
{
try
{
Settings.Serialize(Settings.Instance, "Settings.xml");
Debug.WriteLine("Saved", "Settings");
EventLogger.LogEvent("Saved", "Settings");
}
catch(Exception ex)
{
Debug.WriteLine(ex);
EventLogger.LogException(ex);
}
}
public static bool Load()
{
bool loaded = false;
try
{
if(File.Exists("Settings.xml"))
{
Settings.instance = (Settings)Settings.Deserialize("Settings.xml", typeof(Settings));
loaded = true;
Debug.WriteLine("Loaded", "Settings");
EventLogger.LogEvent("Loaded", "Settings");
}
}
catch(Exception ex)
{
Debug.WriteLine(ex);
EventLogger.LogException(ex);
}
return loaded;
}
private static void Serialize(Object obj, String fileName)
{
try
{
FileStream file = File.Create(fileName);
XmlSerializer xml = new XmlSerializer(obj.GetType());
xml.Serialize(file, obj);
file.Close();
}
catch(Exception ex)
{
Debug.WriteLine(ex);
EventLogger.LogException(ex);
}
}
private static object Deserialize(String fileName, Type objType)
{
object stuff = null;
try //to get data from XML file
{
FileStream file = File.Open(fileName, System.IO.FileMode.Open);
XmlSerializer xml = new XmlSerializer(objType);
stuff = xml.Deserialize(file);
file.Close();
}
catch(Exception ex)
{
Debug.WriteLine(ex);
EventLogger.LogException(ex);
}
return stuff;
}
#endregion Save / Load
}
}
Timothy Bryan