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

Recommended Answers

All 3 Replies

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.

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

I have been using the following class to store my settings for an app.

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;
        }
    }
}

In the other class

Settings mySettings = new Settings("Settings.xml");

mySettings.setSetting("setting_name", "setting_value");
String aSetting = mySettings.getSetting("setting_name");
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.