views:

1644

answers:

3

Does anyone out there have an example of using an embedded Configuration File in either VB.NET or C#? I understand how to embed the configuration file, but what is the best method to access it afterward? Can I treat it as a configuration file, like I can with the external configuration files?

Any help is greatly appreciated.

+4  A: 

i believe it's not the idea to put config file to be an embedded resource. embedded resources get packed into the dll and you can't change your configuration afterwards without recompilation (which is the idea of a config file in a first place).

zappan
Some configurations are only for development purposes, not deployment configuration. This functionality is still needed.
rjarmstrong
well, there are several ways to do it then. my view is that hard-coded constants in a settings (static) class is much quicker in that case, compared to getting values from config file. because embedded resource needs recompilation to change values, it's not a loss to have a class that needs to be recompiled after changing some values in it. other technique to do this is to provide defaults for values not found in config file - that values not need to be specified for deployment, and can be overriden in development environment using non-embedded resource file...
zappan
A: 

One option for you is to use a Configuration object to contain default values for any configuration settings. You could then, in the constructor for the configuration object, attempt to override the default config values with values from somewhere such as app.config, a database configuration table, etc.

This is useful for a number of purposes:

  1. Unit testing tools (such as nUnit) require special configuration to access app.config.
  2. The existance of app.config file is not necessary as it will use the default values (which seems to be your goal).
  3. Can help ensure type-safe configuration values.

A simple example:

public class MyConfiguration
{


    private string _defaultValue1 = "Value1";
    private int _defaultValue2 = 22;



    public string Value1
    {
        get
        {
            return _defaultValue1;
        }
    }

    public int Value2
    {
        get
        {
            return _defaultValue2;
        }
    }

    #region cnstr

    public MyConfiguration()
    {
        LoadValuesFromConfigurationXml();
    }

    #endregion


    public static MyConfiguration GetConfig()
    {
        // Optionally Cache the config values in here... caching code removed 
        // for simplicity
        return new MyConfiguration();
    }    

    internal void LoadValuesFromConfigurationXml()
    {
        int tempInt;

        string value = ConfigurationManager.AppSettings["Value1"];

        if (!String.IsNullOrEmpty(value))
        {
            _defaultValue1 = value;
        }

        value = ConfigurationManager.AppSettings["Value2"];

        if (!String.IsNullOrEmpty(value))
        {
            if (int.TryParse(value, out tempInt))
            {
                _defaultValue2 = tempInt;
            }
        }


    }

}

To access the config values use: MyConfiguration.GetConfig().Value1

Hope this helps, Max

Max Schilling
+1  A: 

I usually have a class that saves preferences in a xml in the UserAppDataPath. Something like:

public class Preferences
    {
        public static String prefFile = "/preferences.xml";
        private static XmlDocument doc;

        public static void set(String prefName, String value)
        {
            XmlElement nnode = (XmlElement)root.SelectSingleNode("//" + prefName);
            if (nnode == null)
            {
                nnode = doc.CreateElement(prefName);
                root.AppendChild(nnode);
            }
            nnode.InnerText = value;
        }
        public static String get(String prefName)
        {
            XmlElement nnode = (XmlElement)root.SelectSingleNode("//" + prefName);
            if (nnode != null) return nnode.InnerText;
            return "";
        }

        private static XmlElement root
        {
            get
            {
                if (doc == null)
                {
                    doc = new XmlDocument();
                    try
                    {
                        doc.Load(Application.UserAppDataPath + prefFile);
                    }
                    catch (Exception)
                    {
                        doc.LoadXml("<root></root>");
                        NodeChangedHandler(null, null); // costringo a salvare.
                    }
                    doc.NodeChanged += new XmlNodeChangedEventHandler(NodeChangedHandler);
                    doc.NodeInserted += new XmlNodeChangedEventHandler(NodeChangedHandler);
                    doc.NodeRemoved += new XmlNodeChangedEventHandler(NodeChangedHandler);
                }
                return (XmlElement)doc.FirstChild;
            }
        }
        private static void NodeChangedHandler(Object src, XmlNodeChangedEventArgs args)
        {
            if (doc != null)
                doc.Save(prefFile);
        }
    }
kajyr