ConfigurationManager.AppSettings使用其他配置文件(Configura

2019-09-02 04:27发布

我在我的班约10个方法。 在每一个方法我用ConfigurationManager.AppSettings获得价值形态App.config文件

喜欢

 _applicationPort = int.Parse(ConfigurationManager.AppSettings["ApplicationPort"]

我的问题是,我想使这个代码像AnotherPoject.exe.config另一个app.config文件得到的AppSettings。

Answer 1:

您还可以设置app.config读取其他文件。 事情是这样的:

<?xml version="1.0"?>
<configuration>
  <appSettings  file="my\custom\file\path\external.config"/>
</configuration>

external.config将有appSettings部分:

<appSettings>
    <add key="myKey" value="myValue" />
</appSettings>

请参阅此MSDN ,了解更多信息。



Answer 2:

你可以做这样的事情

var fileConfig = ConfigurationManager.OpenExeConfiguration("<filePath>");
int port = int.Parse(fileConfig.AppSettings["PortNumber"].ToString());


Answer 3:

您可以通过完成此ConfigurationManager.OpenExeConfiguration 。 这将允许您轻松地打开另一个配置文件。

MSDN文章关于OpenExeConfiguration。



文章来源: ConfigurationManager.AppSettings use another config file