10

我班上有大约 10 种方法。在我ConfigurationManager.AppSettings用来从 App.config 文件中获取值的每种方法中

喜欢

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

我的问题是我想让这段代码从另一个 app.config 文件(如 AnotherPoject.exe.config)中获取 AppSettings。

4

3 回答 3

17

您还可以设置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

于 2015-05-15T09:14:10.793 回答
6

你可以做这样的事情

var fileConfig = ConfigurationManager.OpenExeConfiguration("<filePath>");
int port = int.Parse(fileConfig.AppSettings["PortNumber"].ToString());
于 2013-05-07T17:56:49.307 回答
4

您可以使用ConfigurationManager.OpenExeConfiguration. 这将允许您轻松打开另一个配置文件。

有关 OpenExeConfiguration 的MSDN文章。

于 2013-05-07T17:52:58.467 回答