Configuration File as Embedded Resource

2019-01-23 08:55发布

问题:

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.

回答1:

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).



回答2:

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



回答3:

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