您的位置:首页 > 移动开发

C#项目中关于多个程序集下App.config文件的问题

2013-01-17 15:29 543 查看
在项目中我们会经常用到App.config文件,有的是自动生成的,比如引用webservice、wcf服务时生成;也有手动建立的配置文件直接默认名就为app.config。

这些配置有的保存当前程序集用到的一些可供外部改动的变量,比如:

<configuration>
  <appSettings>
    <add key="keyName" value="value"/>
  </appSettings>
</configuration>

这种的配置直接使用 ConfigurationManager.AppSettings["key名"]来读取比较方便。例如:

public class ReadConfig
    {
        public static string ConfigKeyValue
        {
            string config = ConfigurationManager.AppSettings["ConfigKeyValue"];

            config = string.IsNullOrEmpty(config) ? "空字符串" : config;

            return config;
        }
    }

有表示数据库连接的比如ADO.NET Entity连接数据时会生成配置。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
  </startup>
  <connectionStrings>
    <add name="OracleEntities" connectionString="" />
  </connectionStrings>
</configuration>

有Microsoft.Practices.EnterpriseLibrary连接数据库的配置 (/article/1419452.html)。

再有

”混合模式程序集是针对“v2.0.50727”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行“错误时要用的,

<?xml version="1.0"?>
<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku = ".NETFramework,Version=v4.0"/>
    <supportedRuntime version="v2.0.50727"/>
  </startup>
</configuration>

等等,总之app.config作用很多。

但是在一个项目中很多程序集都要用到app.config该怎么办呢?比如如下情况:



FouctionDll程序集中配置:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="key1" value="你好,世界!"/>
  </appSettings>
</configuration>

主目录TestAppConfig中的配置:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="key2" value="Hello,world!" />
  </appSettings>
</configuration>

这样的情况下,如果主程序集需要引用FouctionDll,配置被复制过来由于配置名重复,自然会被主程序的配置覆盖。

还有就是如果FouctionDll中要引用远程服务,会自动生成app.config,一旦主程序引用该配置依然无法使用。

针对这样,应该做如下解决:

1、第一种情况,每一个程序集的配置是手动增加的话,将起名字改变。读取方式不再使用

ConfigurationManager.AppSettings["key"]来读取,可改为:

public class ReadConfig
    {
        private static string currentConfig = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"ConfigName.config";
        
        /// <summary>
        /// 判断是否有人操作的间隔时间
        /// </summary>
        public static string ConfigKeyValue
        {
            get
            {
                string time =GetAttributeValue(currentConfig,"ConfigKeyValue");

                if (string.IsNullOrEmpty(time))
                {
                    return "180";
                }

                return time;
            }
        }
        /// <summary>
        /// 获取配置文件的属性
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        private static string GetAttributeValue(string file, string key)
        {
            string value = string.Empty;

            try
            {
                if (File.Exists(file))
                {
                    XmlDocument xml = new XmlDocument();

                    xml.Load(file);

                    XmlNode xNode = xml.SelectSingleNode("//appSettings");

                    XmlElement element = (XmlElement)xNode.SelectSingleNode("//add[@key='" + key + "']");

                     value = element.GetAttribute("value").ToString();
                }
            }
            catch { }

            return value;
        }
    }


这种方式的好处是让每个程序集相对独立,缺点是如果是自动生成的app.config则还是会有上述问题。那么对于需要用到的名字必须是app.config的情况该如何呢?

可以使用超链接的方式,就是在项目中只有主程序使用app.config,其他程序集使用它的链接,这样共同使用,如图



读取还是在当前目录下使用ConfigurationManager.AppSettings["keyName"];

这种方式的好处是,可以解决了几个程序集共用一个app.config的问题,缺点是程序集不独立,因为引用了同一个文件,程序集在移动目录是需要重新检查再手动引用。

总之开发软件第一目的是软件功能正常,其次是我们开发时要尽可能的使自己方便,只有更方便,效率才能提高。

代码下载:http://download.csdn.net/detail/yysyangyangyangshan/5004721
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: