[英]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.