在我的项目中,我有两个名为app.config
和accessLevel.config
的应用程序配置文件。现在使用OpenExeConfiguration
我可以访问app.config.exe file
但不能访问accessLevel.config
。请帮忙。
我有2个配置文件的主要原因是显示差异并使代码简单。
我需要阅读accessLevel.config
代码中C#
的值。
尝试以下代码但没有用:
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.File = "App2.config";
答案 0 :(得分:22)
请参阅here。
将其放入App.config
:
<appSettings file="accessLevel.config"/>
然后有另一个名为accessLevel.config的文件,如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
<add key="TestSetting" value="TestValue"/>
</appSettings>
然后您可以使用以下代码访问您的配置值:
string value = ConfigurationManager.AppSettings["TestSetting"];
确保将accessLevel.config设置为复制到输出目录(右键单击Visual Studio中的文件 - &gt;属性 - &gt;复制到输出目录 - &gt;如果更新则复制)。