I'm developing a WinForm application with C# and .NET Framework 4.7.
I want to open a Web.config file, read its appSetting section and modify it.
To open it, I use this:
System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration(null);
It opens it but, when I try to get the keys with:
string[] keys = config.AppSettings.Settings.AllKeys;
I get a null array.
This is the appSetting section:
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
</configSections>
<connectionStrings>
</connectionStrings>
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="MinRemainingCodes" value="100" />
<!-- Others keys -->
</appSettings>
</configuration>
Maybe the problem is that it is not opening the file, but in the documentation say:
The virtual path to the configuration file. If null, the root Web.config file is opened.
Maybe I don't understand what means with root
because the program and the Web.config
file are in the same folder.
What am I doing wrong?