且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在单个项目中从 c# 中的多个配置文件中读取值?

更新时间:2022-12-05 22:57:29

参见 这里.

把它放在你的 App.config 中:

<appSettings file="accessLevel.config"/>

然后有另一个名为 accessLevel.config 的文件,如下所示:

And then have another file called accessLevel.config like this:

<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
  <add key="TestSetting" value="TestValue"/>
</appSettings>

然后你可以在这样的代码中访问你的配置值:

And then you can access your config values in code like this:

string value = ConfigurationManager.AppSettings["TestSetting"];

确保将 accessLevel.config 设置为复制到输出目录(在 Visual Studio 中右键单击文件 -> 属性 -> 复制到输出目录 -> 如果较新则复制).

Make sure that accessLevel.config is set to copy to the output directory (right click the file in Visual Studio -> Properties -> Copy To Output Directory -> Copy if Newer).