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

我class上有大约10种方法。 在每个方法中,我使用ConfigurationManager.AppSettings从App.config文件中获取值

喜欢

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

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

您还可以将app.config设置为读取另一个文件。 像这样的东西:

     

external.config将有appSettings部分:

    

有关其他信息,请参阅此msdn 。

你可以这样做

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

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

有关OpenExeConfiguration的MSDN文章。