c#多个程序集使用app.config 的解决办法

多个程序集使用app.config时,

System.Configuration.ConfigurationManager,指向的只会是主程序集的app.config,

 

假设有个程序集使用为common

添加,app.config,它生成时,变成了common.dll.config

 

被引用的程序集自己不能使用了,

这里,可以这样使用,

System.Configuration.Configuration conf=

ConfigurationManager.OpenExeConfiguration(

//这里需要指定dll路径,因为生成的dll被被netframework调用,路径问题找起来会有点别扭,就是说,如果在程序集代码里写,找本程序集的文件路径的代码,

//我也不知道怎么写,监视了一下也没有看到,因为dll是被netframework调用,显示的路径,是netframework的一个dll的路径(别管它了)
AppDomain.CurrentDomain.BaseDirectory + "bin\\" + typeof(CommonConfiger).Assembly.ManifestModule.ScopeName // + ".config",这里不需要.config,它会自己加,
);

//其中,CommonConfiger为common项目中的一个类,

//typeof(CommonConfiger).Assembly.ManifestModule.ScopeName,这个就是common.dll

//然后它会自己打开被引用后,打开,web站点下\bin\common.dll.config   (注意,这个文件你可能需要自己拷过去,生成好像不能直接生成过去)

 

使用 appSettings

1。add配置,app.config里的配置是一样的,不说了,

2。使用时,不再是ConfigurationManager.AppSettings["key1"]了,

而是 conf.AppSettings.Settings["key1"].Value

 

。。。

 

posted @ 2017-07-25 17:18  以函  阅读(602)  评论(0)    收藏  举报