Click here to Skip to main content
15,914,488 members
Home / Discussions / C#
   

C#

 
GeneralRe: how to convert list to dictionary Pin
Dan Neely31-Jul-09 9:26
Dan Neely31-Jul-09 9:26 
AnswerRe: how to convert list to dictionary Pin
Super Lloyd31-Jul-09 4:18
Super Lloyd31-Jul-09 4:18 
GeneralRe: how to convert list to dictionary Pin
hotthoughtguy31-Jul-09 4:28
hotthoughtguy31-Jul-09 4:28 
AnswerRe: how to convert list to dictionary Pin
0x3c031-Jul-09 4:38
0x3c031-Jul-09 4:38 
GeneralRe: how to convert list to dictionary Pin
hotthoughtguy31-Jul-09 4:44
hotthoughtguy31-Jul-09 4:44 
AnswerRe: how to convert list to dictionary Pin
Dave Kreskowiak31-Jul-09 5:05
mveDave Kreskowiak31-Jul-09 5:05 
QuestionProgrammatically updating application properties or help creating my own xml file please. Pin
JollyMansArt31-Jul-09 2:51
JollyMansArt31-Jul-09 2:51 
AnswerRe: Programmatically updating application properties or help creating my own xml file please. Pin
Super Lloyd31-Jul-09 2:55
Super Lloyd31-Jul-09 2:55 
Here you go, an (inline) alternative to App Config
(tested and used a lot.. I'm just too lazy to make an article about it...)

<br />
	public class ConfigFile<br />
	{<br />
		#region GetDefaultPath(..)<br />
<br />
		public static string GetDefaultPath()<br />
		{<br />
			return GetDefaultPath(ConfigurationFolder.LocalApplicationData, null);<br />
		}<br />
		public static string GetDefaultPath(ConfigurationFolder folder)<br />
		{<br />
			return GetDefaultPath(folder, null);<br />
		}<br />
		public static string GetDefaultPath(ConfigurationFolder folder, string name)<br />
		{<br />
			string dir = AppInfo.GetDataDirectory(folder);<br />
			if (string.IsNullOrEmpty(name))<br />
				name = AppInfo.AppName;<br />
			return Path.Combine(dir, string.Format("{0}.config", name.PathSafe()));<br />
		}<br />
<br />
		#endregion<br />
<br />
		#region GetShared config files<br />
<br />
		public static ConfigFile GetShared()<br />
		{<br />
			return GetShared(GetDefaultPath());<br />
		}<br />
		public static ConfigFile GetShared(ConfigurationFolder folder)<br />
		{<br />
			return GetShared(GetDefaultPath(folder));<br />
		}<br />
		public static ConfigFile GetShared(ConfigurationFolder folder, string name)<br />
		{<br />
			return GetShared(GetDefaultPath(folder, name));<br />
		}<br />
		public static ConfigFile GetShared(string path)<br />
		{<br />
			lock (configFiles)<br />
			{<br />
				ConfigFile result;<br />
				if (!configFiles.TryGetValue(path, out result))<br />
				{<br />
					result = new ConfigFile(path);<br />
					configFiles[path] = result;<br />
				}<br />
				return result;<br />
			}<br />
		}<br />
<br />
		static Dictionary<string, ConfigFile> configFiles = new Dictionary<string, ConfigFile>();<br />
<br />
		public static void SaveAll()<br />
		{<br />
			lock (configFiles)<br />
			{<br />
				foreach (var cf in configFiles.Values)<br />
					cf.Save();<br />
			}<br />
		}<br />
<br />
		#endregion<br />
<br />
		#region instance methods: Save(), Reload(), GetSection<T>()<br />
<br />
		XDict config = new XDict();<br />
<br />
		public ConfigFile(string path)<br />
		{<br />
			FilePath = path;<br />
			if (string.IsNullOrEmpty(path))<br />
				throw new ArgumentNullException("path");<br />
			Reload();<br />
		}<br />
<br />
		public string FilePath { get; private set; }<br />
<br />
		public void Save()<br />
		{<br />
			string dir = Path.GetDirectoryName(FilePath);<br />
			if (!Directory.Exists(dir))<br />
				Directory.CreateDirectory(dir);<br />
<br />
			XDocument doc = new XDocument(config.ToXNode());<br />
			doc.Save(FilePath);<br />
		}<br />
<br />
		public void Reload()<br />
		{<br />
			if (File.Exists(FilePath))<br />
				try<br />
				{<br />
					XDocument doc = XDocument.Load(FilePath);<br />
					XElement xdict = doc.Root;<br />
					config = new XDict();<br />
					if (xdict != null)<br />
						config.FromXNode(xdict);<br />
				}<br />
				catch (InvalidOperationException) { }<br />
				catch (XmlException) { }<br />
		}<br />
<br />
		public XDict GetSection<T>()<br />
		{<br />
			string key = typeof(T).FullName;<br />
			XDict section = config.Get<XDict>(key);<br />
			if (section == null)<br />
			{<br />
				section = new XDict();<br />
				config[key] = section;<br />
			}<br />
			return section;<br />
		}<br />
<br />
		#endregion<br />
	}<br />
<br />
	public interface IXObject : IXmlSerializable<br />
	{<br />
		XNode ToXNode();<br />
		void FromXNode(XNode node);<br />
	}<br />
<br />
	public class XValue : IXObject<br />
	{<br />
		public XValue() { }<br />
		public XValue(string s) { StringValue = s; }<br />
<br />
		public string StringValue { get; set; }<br />
<br />
		public override string ToString()<br />
		{<br />
			return StringValue;<br />
		}<br />
<br />
		#region Strongly Typed Value<br />
<br />
		public T Get<T>()<br />
		{<br />
			return Get<T>(default(T));<br />
		}<br />
		public T Get<T>(T defaultVal)<br />
		{<br />
			T ret;<br />
			if (!string.IsNullOrEmpty(StringValue))<br />
			{<br />
				if (Converter.TryGetTValue<T>(StringValue, out ret))<br />
					return ret;<br />
				// incorrect value? => remove it<br />
				StringValue = null;<br />
			}<br />
			return defaultVal;<br />
		}<br />
<br />
		public void Set<T>(T val)<br />
		{<br />
			Set<T>(val, default(T));<br />
		}<br />
		public void Set<T>(T val, T defaultVal)<br />
		{<br />
			if (Equals(val, defaultVal))<br />
			{<br />
				StringValue = null;<br />
			}<br />
			else<br />
			{<br />
				string ret;<br />
				if (Converter.TryGetStringValue<T>(val, out ret))<br />
					StringValue = ret;<br />
			}<br />
		}<br />
<br />
		#endregion<br />
<br />
		#region static IXObject Create(XNode node)<br />
<br />
		internal static IXObject Create(XNode node)<br />
		{<br />
			if (node == null)<br />
				return null;<br />
			if (node is XText)<br />
			{<br />
				XValue val = new XValue();<br />
				val.FromXNode(node);<br />
				return val;<br />
			}<br />
			else if (node is XElement)<br />
			{<br />
				XElement xe = (XElement)node;<br />
				switch (xe.Name.LocalName)<br />
				{<br />
					case "list":<br />
						XList xl = new XList();<br />
						xl.FromXNode(xe);<br />
						return xl;<br />
					case "dictionary":<br />
						XDict xd = new XDict();<br />
						xd.FromXNode(xe);<br />
						return xd;<br />
				}<br />
			}<br />
			return null;<br />
		}<br />
<br />
		#endregion<br />
<br />
		#region IXObject Members<br />
<br />
		public XNode ToXNode()<br />
		{<br />
			var s = StringValue;<br />
			if (s == null)<br />
				s = "";<br />
			return new XText(s);<br />
		}<br />
<br />
		public void FromXNode(XNode node)<br />
		{<br />
			XText t = node as XText;<br />
			if (t != null)<br />
				StringValue = t.Value;<br />
		}<br />
<br />
		#endregion<br />
<br />
		#region IXmlSerializable Members<br />
<br />
		public XmlSchema GetSchema()<br />
		{<br />
			return null;<br />
		}<br />
<br />
		public void ReadXml(XmlReader reader)<br />
		{<br />
			try<br />
			{<br />
				// when coming from an AppSettings, there is an extra tag while reading<br />
				reader.Read();<br />
				XNode node = XNode.ReadFrom(reader);<br />
				FromXNode(node);<br />
			}<br />
			catch (InvalidOperationException) { }<br />
			catch (XmlException) { }<br />
		}<br />
<br />
		public void WriteXml(XmlWriter writer)<br />
		{<br />
			ToXNode().WriteTo(writer);<br />
		}<br />
<br />
		#endregion<br />
	}<br />
<br />
	public class XList : List<IXObject>, IXObject<br />
	{<br />
		public override string ToString()<br />
		{<br />
			StringBuilder sb = new StringBuilder("[");<br />
			for (int i = 0; i < 10 && i < Count; i++)<br />
			{<br />
				if (i > 0)<br />
					sb.Append(", ");<br />
				sb.Append(this[i]);<br />
			}<br />
			if (Count > 9)<br />
				sb.Append(", ...");<br />
			sb.Append("]");<br />
			return sb.ToString(); ;<br />
		}<br />
<br />
		#region IXObject Members<br />
<br />
		public XNode ToXNode()<br />
		{<br />
			XElement top = new XElement("list");<br />
			foreach (var item in this)<br />
			{<br />
				top.Add(new XElement("item", item.ToXNode()));<br />
			}<br />
			return top;<br />
		}<br />
<br />
		public void FromXNode(XNode node)<br />
		{<br />
			XElement xe = node as XElement;<br />
			if (xe == null || xe.Name != "list")<br />
				return;<br />
			foreach (XElement item in xe.Elements("item"))<br />
			{<br />
				XNode itemNode = item.FirstNode;<br />
				Add(XValue.Create(itemNode));<br />
			}<br />
		}<br />
<br />
		#endregion<br />
<br />
		#region IXmlSerializable Members<br />
<br />
		public XmlSchema GetSchema()<br />
		{<br />
			return null;<br />
		}<br />
<br />
		public void ReadXml(XmlReader reader)<br />
		{<br />
			try<br />
			{<br />
				// when coming from an AppSettings, there is an extra tag while reading<br />
				reader.Read();<br />
				XNode node = XNode.ReadFrom(reader);<br />
				FromXNode(node);<br />
			}<br />
			catch (InvalidOperationException) { }<br />
			catch (XmlException) { }<br />
		}<br />
<br />
		public void WriteXml(XmlWriter writer)<br />
		{<br />
			ToXNode().WriteTo(writer);<br />
		}<br />
<br />
		#endregion<br />
	}<br />
	public class XDict : Dictionary<string, IXObject>, IXObject<br />
	{<br />
		public override string ToString()<br />
		{<br />
			StringBuilder sb = new StringBuilder("{");<br />
			int i = 0;<br />
			foreach (var item in this)<br />
			{<br />
				if (i > 0)<br />
					sb.Append(", ");<br />
				i++;<br />
				sb.Append(item.Key);<br />
				sb.Append(": ");<br />
				sb.Append(item.Value);<br />
			}<br />
			if (Count > 9)<br />
				sb.Append(", ...");<br />
			sb.Append("}");<br />
			return sb.ToString(); ;<br />
		}<br />
<br />
		public new IXObject this[string key]<br />
		{<br />
			get<br />
			{<br />
				IXObject result;<br />
				TryGetValue(key, out result);<br />
				return result;<br />
			}<br />
			set<br />
			{<br />
				if (value == null)<br />
					Remove(key);<br />
				else<br />
					base[key] = value;<br />
			}<br />
		}<br />
<br />
		#region Strongly Typed Helper<br />
<br />
		public T Get<T>(string key)<br />
		{<br />
			return Get<T>(key, default(T));<br />
		}<br />
		public T Get<T>(string key, T defaultVal)<br />
		{<br />
			IXObject xo;<br />
			TryGetValue(key, out xo);<br />
			if (typeof(IXObject).IsAssignableFrom(typeof(T)))<br />
			{<br />
				if (xo is T && xo != null)<br />
					return (T)xo;<br />
				return defaultVal;<br />
			}<br />
<br />
			XValue xv = xo as XValue;<br />
			T ret;<br />
			if (xv != null && !string.IsNullOrEmpty(xv.StringValue))<br />
			{<br />
				if (Converter.TryGetTValue<T>(xv.StringValue, out ret))<br />
					return ret;<br />
			}<br />
			// not what thought? => remove it<br />
			Remove(key);<br />
			return defaultVal;<br />
		}<br />
<br />
		public void Set<T>(string key, T val)<br />
		{<br />
			Set<T>(key, val, default(T));<br />
		}<br />
		public void Set<T>(string key, T val, T defaultVal)<br />
		{<br />
			if (typeof(IXObject).IsAssignableFrom(typeof(T)))<br />
			{<br />
				IXObject xo = val != null ? (IXObject)val : (IXObject)defaultVal;<br />
				this[key] = xo;<br />
				return;<br />
			}<br />
<br />
			XValue xval;<br />
			if (Equals(val, defaultVal))<br />
			{<br />
				xval = null;<br />
			}<br />
			else<br />
			{<br />
				string ret;<br />
				if (Converter.TryGetStringValue<T>(val, out ret))<br />
					xval = new XValue(ret);<br />
				else<br />
					xval = null;<br />
			}<br />
			if (xval == null)<br />
				Remove(key);<br />
			else<br />
				this[key] = xval;<br />
		}<br />
<br />
		#endregion<br />
<br />
		#region IXObject Members<br />
<br />
		public XNode ToXNode()<br />
		{<br />
			XElement top = new XElement("dictionary");<br />
			foreach (var item in this.Keys)<br />
			{<br />
				XElement xitem = new XElement("item", this[item].ToXNode());<br />
				xitem.Add(new XAttribute("key", item));<br />
				top.Add(xitem);<br />
			}<br />
			return top;<br />
		}<br />
<br />
		public void FromXNode(XNode node)<br />
		{<br />
			XElement xe = node as XElement;<br />
			if (xe == null || xe.Name != "dictionary")<br />
				return;<br />
			foreach (XElement item in xe.Elements("item"))<br />
			{<br />
				string name = item.Attribute("key").Value;<br />
				XNode itemNode = item.FirstNode;<br />
				this[name] = XValue.Create(itemNode);<br />
			}<br />
		}<br />
<br />
		#endregion<br />
<br />
		#region IXmlSerializable Members<br />
<br />
		public XmlSchema GetSchema()<br />
		{<br />
			return null;<br />
		}<br />
<br />
		public void ReadXml(XmlReader reader)<br />
		{<br />
			try<br />
			{<br />
				// when coming from an AppSettings, there is an extra tag while reading<br />
				reader.Read();<br />
				XNode node = XNode.ReadFrom(reader);<br />
				FromXNode(node);<br />
			}<br />
			catch (InvalidOperationException) { }<br />
			catch (XmlException) { }<br />
		}<br />
<br />
		public void WriteXml(XmlWriter writer)<br />
		{<br />
			ToXNode().WriteTo(writer);<br />
		}<br />
<br />
		#endregion<br />
	}<br />
<br />


A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station....
_________________________________________________________
My programs never have bugs, they just develop random features.

GeneralRe: Programmatically updating application properties or help creating my own xml file please. Pin
Henry Minute31-Jul-09 3:57
Henry Minute31-Jul-09 3:57 
GeneralRe: Programmatically updating application properties or help creating my own xml file please. Pin
Super Lloyd31-Jul-09 4:03
Super Lloyd31-Jul-09 4:03 
JokeRe: Programmatically updating application properties or help creating my own xml file please. Pin
JollyMansArt1-Aug-09 2:33
JollyMansArt1-Aug-09 2:33 
GeneralRe: Programmatically updating application properties or help creating my own xml file please. Pin
Super Lloyd1-Aug-09 16:53
Super Lloyd1-Aug-09 16:53 
Questionnot able to view the form controls when thread is running [modified] Pin
Vivek Vijayan31-Jul-09 2:37
Vivek Vijayan31-Jul-09 2:37 
AnswerRe: not able to view the form controls when thread is running Pin
stancrm31-Jul-09 3:30
stancrm31-Jul-09 3:30 
QuestionHow to increase Web Service performance ? Pin
hdv21231-Jul-09 2:35
hdv21231-Jul-09 2:35 
AnswerRe: How to increase Web Service performance ? Pin
Dave Kreskowiak31-Jul-09 4:58
mveDave Kreskowiak31-Jul-09 4:58 
GeneralRe: How to increase Web Service performance ? Pin
hdv21231-Jul-09 6:37
hdv21231-Jul-09 6:37 
GeneralRe: How to increase Web Service performance ? Pin
Dave Kreskowiak31-Jul-09 7:28
mveDave Kreskowiak31-Jul-09 7:28 
GeneralRe: How to increase Web Service performance ? Pin
hdv2121-Aug-09 9:21
hdv2121-Aug-09 9:21 
GeneralRe: How to increase Web Service performance ? Pin
Dave Kreskowiak2-Aug-09 6:12
mveDave Kreskowiak2-Aug-09 6:12 
QuestionMiddle mouse click and scroll Pin
gwithey31-Jul-09 1:05
gwithey31-Jul-09 1:05 
AnswerRe: Middle mouse click and scroll Pin
gwithey31-Jul-09 1:27
gwithey31-Jul-09 1:27 
GeneralRe: Middle mouse click and scroll Pin
Luc Pattyn31-Jul-09 1:38
sitebuilderLuc Pattyn31-Jul-09 1:38 
GeneralRe: Middle mouse click and scroll Pin
Baeltazor31-Jul-09 7:35
Baeltazor31-Jul-09 7:35 
GeneralRe: Middle mouse click and scroll Pin
gwithey2-Aug-09 20:51
gwithey2-Aug-09 20:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.