簡體   English   中英

ConfigurationManager.AppSettings 使用另一個配置文件

[英]ConfigurationManager.AppSettings use another config file

我的班級大約有 10 種方法。 在每種方法中,我都使用ConfigurationManager.AppSettings從 App.config 文件中獲取值

喜歡

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

我的問題是我想讓這段代碼從另一個 app.config 文件(如 AnotherPoject.exe.config)中獲取 AppSettings。

您還可以將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

你可以做這樣的事情

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

您可以通過使用ConfigurationManager.OpenExeConfiguration來完成此ConfigurationManager.OpenExeConfiguration 這將允許您輕松打開另一個配置文件。

關於 OpenExeConfiguration 的MSDN文章。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM