【问题标题】:Configuration File as Embedded Resource配置文件作为嵌入式资源
【发布时间】:2010-10-02 12:46:21
【问题描述】:

有没有人有在 VB.NET 或 C# 中使用嵌入式配置文件的示例?我了解如何嵌入配置文件,但之后访问它的最佳方法是什么?我可以像对待外部配置文件一样将其视为配置文件吗?

非常感谢任何帮助。

【问题讨论】:

    标签: .net configuration


    【解决方案1】:

    我认为将配置文件作为嵌入式资源并不是一个好主意。嵌入式资源被打包到 dll 中,之后您无法在不重新编译的情况下更改配置(这首先是配置文件的想法)。

    【讨论】:

    • 有些配置仅用于开发目的,并非部署配置。仍然需要此功能。
    • 好吧,那么有几种方法可以做到这一点。我的观点是,与从配置文件中获取值相比,在这种情况下,设置(静态)类中的硬编码常量要快得多。因为嵌入式资源需要重新编译才能更改值,所以拥有一个在更改其中的某些值后需要重新编译的类并不是一种损失。执行此操作的其他技术是为配置文件中未找到的值提供默认值 - 不需要为部署指定值,并且可以在开发环境中使用非嵌入式资源文件覆盖...
    • 我怎样才能拥有一个可以在所有 .net 框架中运行的 exe?有没有办法在 exe 本身中嵌入 app.config 字符串?
    【解决方案2】:

    您的一个选择是使用配置对象来包含任何配置设置的默认值。然后,您可以在配置对象的构造函数中尝试使用 app.config、数据库配置表等某处的值覆盖默认配置值。

    这可用于多种用途:

    1. 单元测试工具(例如 nUnit)需要特殊配置才能访问 app.config。
    2. app.config 文件的存在不是必需的,因为它将使用默认值(这似乎是您的目标)。
    3. 可以帮助确保类型安全的配置值。

    一个简单的例子:

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

    要访问配置值,请使用:MyConfiguration.GetConfig().Value1

    希望这会有所帮助, 最大

    【讨论】:

    • 为什么不加评论就投反对票?如果上面的代码有问题,我很想知道原因,但是随机的否决不是很有帮助......
    • 赞成,因为我认为这是一种结合上述方法的优雅方式,即拥有任何类型的默认配置文件和一个内部类来处理默认值以及加载和保存配置文件......但是我将它实现为一个内部静态类,因为我想不出有什么好的理由让两个单独的配置同时处于活动状态。
    【解决方案3】:

    我通常有一个类将首选项保存在 UserAppDataPath 中的 xml 中。 比如:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-30
      • 1970-01-01
      • 1970-01-01
      • 2013-03-06
      • 1970-01-01
      相关资源
      最近更新 更多