| | |
Settings.settings thing
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2006
Posts: 436
Reputation:
Solved Threads: 72
IMO you should use an XML file to store extra information. I don't particularly like the Settings way in dotnet simply because it has some ugly side effects. The settings data is stored in the Documents and Settings area, and if you move the application to a different directory, you will leave trash data behind in the original directory, and the settings data does not follow to the new directory. Second, If you decide to change a setting name in the future, you have another problem of how to upgrade their existing settings without losing data.
After being bit a few times with Settings, I never use them anymore. XML is easy to use and expand.
But if I have not convinced you to avoid Settings, then look in the designer code of your project and see how the settings are being initialized, and that should let you perform the same steps at run time.
After being bit a few times with Settings, I never use them anymore. XML is easy to use and expand.
But if I have not convinced you to avoid Settings, then look in the designer code of your project and see how the settings are being initialized, and that should let you perform the same steps at run time.
•
•
Join Date: Jan 2009
Posts: 24
Reputation:
Solved Threads: 1
I have been using the following class to store my settings for an app.
In the other class
C# Syntax (Toggle Plain Text)
using System; using System.Collections.Generic; using System.Text; using System.Xml; using System.IO; namespace Alarm.Plugins.Speech { class Settings { private string xmlFile; private XmlDocument xmldoc = new XmlDocument(); /// <summary> /// Constructor for xml manager /// </summary> /// <param name="xmlFile">File name of the xml file</param> public Settings(string xmlFile) { this.xmlFile = xmlFile; initXml(); } private bool initXml() { try { xmldoc.Load(xmlFile); } catch (Exception) { TextWriter writer = new StreamWriter(xmlFile); writer.WriteLine("<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>"); writer.WriteLine("<Settings>"); writer.WriteLine("</Settings>"); writer.Close(); } return true; } /// <summary> /// Returns a setting from the settings.xml /// </summary> /// <param name="settingNode">Node to return value of</param> /// <returns>String: setting</returns> public string getSetting(string settingNode) { xmldoc.Load(xmlFile); XmlNodeList list = xmldoc.GetElementsByTagName("Settings"); try { foreach (XmlNode searchNode in list) { return searchNode.SelectSingleNode(settingNode.Replace(" ", "_")).InnerText; } } catch (Exception) {} return ""; } /// <summary> /// Applies a setting to the xml file /// </summary> /// <param name="settingNode">Node in file to update</param> /// <param name="newValue">Value to update node to</param> /// <returns>true if successful</returns> public bool setSetting(string settingNode, string newValue) { xmldoc.Load(xmlFile); XmlNodeList list = xmldoc.SelectNodes("Settings"); try { foreach (XmlNode searchNode in list) { XmlNode node = searchNode.SelectSingleNode(settingNode.Replace(" ", "_")); if (node == null) { XmlElement element = xmldoc.CreateElement(settingNode.Replace(" ", "_")); xmldoc.DocumentElement.AppendChild(element); searchNode.SelectSingleNode(settingNode.Replace(" ", "_")).InnerText = newValue; }else searchNode.SelectSingleNode(settingNode.Replace(" ", "_")).InnerText = newValue; } } catch (Exception) {return false;} xmldoc.Save(xmlFile); return true; } } }
C# Syntax (Toggle Plain Text)
Settings mySettings = new Settings("Settings.xml"); mySettings.setSetting("setting_name", "setting_value"); String aSetting = mySettings.getSetting("setting_name");
Last edited by metalla_nz; Apr 2nd, 2009 at 12:06 am.
![]() |
Similar Threads
- Storing settings in Visual C++ 2008 (C++)
- Network connection settings problems (Networking Hardware Configuration)
- IE - MSN prob, security settings (Web Browsers)
- Microsoft MN-700 G Band Router acting up (Networking Hardware Configuration)
- xp + folder settings = crap :( (Windows NT / 2000 / XP)
- Problems with mouse and settings (Viruses, Spyware and other Nasties)
- texts/writings appears too sharp (Troubleshooting Dead Machines)
- network settings for a game (Networking Hardware Configuration)
Other Threads in the C# Forum
- Previous Thread: How to use custom type converter to convert 'implicitly'
- Next Thread: I need help to make Analog clock in C#
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access ado.net algorithm array bitmap box broadcast buttons c# chat check checkbox class client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development draganddrop drawing encryption enum event excel file files form format forms ftp function gdi+ httpwebrequest image index install java label list listbox listener login mandelbrot math mouseclick mysql networking object operator oracle path photoshop picturebox pixelinversion post prime programming radians regex remote remoting resource richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation visualstudio webbrowser windows winforms wpf xml






