C#:管理多个文件的App.config [英] C#: manage Multiple App.config files

查看:180
本文介绍了C#:管理多个文件的App.config的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临的一个问题。

我有与Web服务交互的DLL,并将它保存在其app.config文件的端点配置。

I have a dll which is interacting with a webservice and it saves its endpoint configuration in its app.config file.

我想用这个DLL从我的主机应用程序。主机应用程序有它自己的配置文件。我有合并dll的配置内容主办每一次的config当我改变服务端点。

I want to use this dll from my host application. The host application has its own config file. I have to merge the contents of dll's config to host's config each time when I change service endpoint.

有没有办法,我可以同时使用的配置文件的方式。因此,DLL使用其自己的配置,而主机应用程序使用它自己的配置。

Is there a way that I can use both config files. So the dll uses its own config whereas host application use its own config.

推荐答案

配置文件可以包含外部文件。

config files can include external files.

如果你把端点配置到外部文件,然后将其包含在YOUT主机,您将不需要每次

If you'd put the endpoint's config onto an external file, and then include it in yout host, you won't need to change the host's config every time

如:
在你的app.config文件:

eg: in your app.config file:

...
<configSections>
  ...
  <section name="myEndpoint" type="System.Configuration.DictionarySectionHandler" />
  ...
</configSections>
...
<myEndpoint configSource="myEndpoint.config" />



然后myEndpoint.config可能看起来像:

then myEndpoint.config could look like that:

<?xml version="1.0" encoding="utf-8"?>
<myEndpoint>
  <add key="myKey" value="myValue" />
</myEndpoint>

和您可以从您的代码访问值,类似于阅读正常的应用程序的设置,像:

and you can access the values from your code, similarly to reading normal app settings, like that:

var myEndpointConfig = (Hashtable)ConfigurationManager.GetSection("myEndpoint");
Console.WriteLine(myEndpointConfig["myKey"]);

这篇关于C#:管理多个文件的App.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆