Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Global Objects
Message
De
04/09/2007 14:17:39
 
 
À
04/09/2007 10:27:29
Timothy Bryan
Sharpline Consultants
Conroe, Texas, États-Unis
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Titre:
Versions des environnements
Environment:
C# 2.0
OS:
Windows XP SP2
Network:
Windows 2000 Server
Database:
MS SQL Server
Divers
Thread ID:
01251812
Message ID:
01252284
Vues:
13
>John,
>I would be happy to provide you with a simple settings class that uses the singleton design pattern. I have later versions that use other storage medium and also keep the actually settings seperated out. This is the simple version. My Settings stuff is alll in its own library called SettingsLib. To use this you just do the following:
>use the library

Thanks!!! I fixed the bad tag here so I could copy it out.
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
	}
}
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform