Settings.settings thing

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2009
Posts: 4
Reputation: Kloukip is an unknown quantity at this point 
Solved Threads: 0
Kloukip Kloukip is offline Offline
Newbie Poster

Settings.settings thing

 
0
  #1
Mar 31st, 2009
I have a simple question: How do I insert a new entry into settings.settings from the code (not through the table interface) so that i can make user defined presets/settings?
Thanks for any input
Klo
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 436
Reputation: JerryShaw is on a distinguished road 
Solved Threads: 72
JerryShaw JerryShaw is offline Offline
Posting Pro in Training

Re: Settings.settings thing

 
0
  #2
Apr 1st, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 4
Reputation: Kloukip is an unknown quantity at this point 
Solved Threads: 0
Kloukip Kloukip is offline Offline
Newbie Poster

Re: Settings.settings thing

 
0
  #3
Apr 1st, 2009
Thanks for your advice, I didn't understand xml too well at first but will look at it some more then. I was watching in the Settings structures and so but with no result I hope the xml way is really easy as you're saying
Thanks again
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 24
Reputation: metalla_nz has a little shameless behaviour in the past 
Solved Threads: 1
metalla_nz metalla_nz is offline Offline
Newbie Poster

Re: Settings.settings thing

 
0
  #4
Apr 2nd, 2009
I have been using the following class to store my settings for an app.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Xml;
  5. using System.IO;
  6.  
  7. namespace Alarm.Plugins.Speech
  8. {
  9. class Settings
  10. {
  11. private string xmlFile;
  12. private XmlDocument xmldoc = new XmlDocument();
  13.  
  14. /// <summary>
  15. /// Constructor for xml manager
  16. /// </summary>
  17. /// <param name="xmlFile">File name of the xml file</param>
  18. public Settings(string xmlFile)
  19. {
  20. this.xmlFile = xmlFile;
  21. initXml();
  22. }
  23.  
  24. private bool initXml()
  25. {
  26. try
  27. {
  28. xmldoc.Load(xmlFile);
  29. }
  30. catch (Exception)
  31. {
  32. TextWriter writer = new StreamWriter(xmlFile);
  33. writer.WriteLine("<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>");
  34. writer.WriteLine("<Settings>");
  35. writer.WriteLine("</Settings>");
  36. writer.Close();
  37. }
  38. return true;
  39. }
  40. /// <summary>
  41. /// Returns a setting from the settings.xml
  42. /// </summary>
  43. /// <param name="settingNode">Node to return value of</param>
  44. /// <returns>String: setting</returns>
  45. public string getSetting(string settingNode)
  46. {
  47. xmldoc.Load(xmlFile);
  48. XmlNodeList list = xmldoc.GetElementsByTagName("Settings");
  49. try
  50. {
  51. foreach (XmlNode searchNode in list)
  52. {
  53. return searchNode.SelectSingleNode(settingNode.Replace(" ", "_")).InnerText;
  54. }
  55. }
  56. catch (Exception)
  57. {}
  58. return "";
  59. }
  60.  
  61. /// <summary>
  62. /// Applies a setting to the xml file
  63. /// </summary>
  64. /// <param name="settingNode">Node in file to update</param>
  65. /// <param name="newValue">Value to update node to</param>
  66. /// <returns>true if successful</returns>
  67. public bool setSetting(string settingNode, string newValue)
  68.  
  69. {
  70. xmldoc.Load(xmlFile);
  71. XmlNodeList list = xmldoc.SelectNodes("Settings");
  72. try
  73. {
  74. foreach (XmlNode searchNode in list)
  75. {
  76. XmlNode node = searchNode.SelectSingleNode(settingNode.Replace(" ", "_"));
  77. if (node == null)
  78. {
  79. XmlElement element = xmldoc.CreateElement(settingNode.Replace(" ", "_"));
  80. xmldoc.DocumentElement.AppendChild(element);
  81. searchNode.SelectSingleNode(settingNode.Replace(" ", "_")).InnerText = newValue;
  82. }else
  83. searchNode.SelectSingleNode(settingNode.Replace(" ", "_")).InnerText = newValue;
  84.  
  85. }
  86. }
  87. catch (Exception)
  88. {return false;}
  89. xmldoc.Save(xmlFile);
  90. return true;
  91. }
  92. }
  93. }
In the other class
  1. Settings mySettings = new Settings("Settings.xml");
  2.  
  3. mySettings.setSetting("setting_name", "setting_value");
  4. String aSetting = mySettings.getSetting("setting_name");
Last edited by metalla_nz; Apr 2nd, 2009 at 12:06 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC