Search code examples
c#appsettingsconfigurationmanagerwebconfigurationmanager

Read appSettings section from Web.config file using WebConfigurationManager


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?


Solution

  • WebConfigurationManager.OpenWebConfiguration includes the following description of the path parameter:

    The virtual path to the configuration file. If null, the root Web.config file is opened.

    Because your application is not running under IIS as a Web Site, the Web.config that is being opened is actually that which lives in the .NET Framework installation folder itself (in my case, that's C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\web.config).

    WebConfigurationManager.OpenMappedWebConfiguration allows you to map virtual directories to physical directories in order to allow you to specify a virtual path that is mapped to your own local directory. Here's the code I've used to make this work:

    var webConfigurationFileMap = new WebConfigurationFileMap();
    
    webConfigurationFileMap.VirtualDirectories.Add(
        string.Empty,
        new VirtualDirectoryMapping(Directory.GetCurrentDirectory(), isAppRoot: true));
    
    var webConfig = WebConfigurationManager.OpenMappedWebConfiguration(
        webConfigurationFileMap,
        string.Empty);
    

    As you can see, I'm mapping the root virtual directory (using string.Empty) to the application's directory (using Directory.GetCurrentDirectory).