2013-05-07 60 views
5

我的班級中有大約10種方法。在每一個我用ConfigurationManager.AppSettings獲得方法價值形態app.config文件ConfigurationManager.AppSettings使用另一個配置文件

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

我的問題是,我想使這個代碼像AnotherPoject.exe.config另一個app.config文件中得到的AppSettings 。

回答

1

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

MSDN關於OpenExeConfiguration的文章。

5

你可以做這樣的事情

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

'fileConfig.AppSettings.Settings [「PortNumber」]。ToString()'是我需要的 – StingyJack 2017-07-09 14:58:18

10

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

this msdn,瞭解更多信息。

相關問題