Tar*_*ied 7 c# configuration app-config
我班上有大约10种方法.在我使用的每个方法ConfigurationManager.AppSettings中获取值形式的App.config文件
喜欢
_applicationPort = int.Parse(ConfigurationManager.AppSettings["ApplicationPort"]
Run Code Online (Sandbox Code Playgroud)
我的问题是我想让这个代码从另一个app.config文件中获取AppSettings,如AnotherPoject.exe.config.
Tob*_*bon 15
您还可以设置app.config读取另一个文件.像这样的东西:
<?xml version="1.0"?>
<configuration>
<appSettings file="my\custom\file\path\external.config"/>
</configuration>
Run Code Online (Sandbox Code Playgroud)
并且external.config将有appSettings部分:
<appSettings>
<add key="myKey" value="myValue" />
</appSettings>
Run Code Online (Sandbox Code Playgroud)
有关其他信息,请参阅此msdn.
您可以通过使用来完成此操作ConfigurationManager.OpenExeConfiguration。这将允许您轻松打开另一个配置文件。
有关 OpenExeConfiguration 的MSDN文章。
你可以这样做
var fileConfig = ConfigurationManager.OpenExeConfiguration("<filePath>");
int port = int.Parse(fileConfig.AppSettings["PortNumber"].ToString());
Run Code Online (Sandbox Code Playgroud)