3

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?

VansFannel
  • 45,055
  • 107
  • 359
  • 626
  • Does the following example work for you? [WebConfigurationManager.AppSettings](https://learn.microsoft.com/en-us/dotnet/api/system.web.configuration.webconfigurationmanager.appsettings?view=netframework-4.7.2#examples) – IronAces Aug 30 '18 at 09:10
  • try `string[] keys =ConfigurationManager.AppSettings.AllKeys?` – A_Sk Aug 30 '18 at 09:14
  • @DanielShillcock Thanks, but it doesn't work. I have updated my question with more details. – VansFannel Aug 30 '18 at 09:24

3 Answers3

4

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).

Kirk Larkin
  • 84,915
  • 16
  • 214
  • 203
0

OpenWebConfiguration is supposed to receive the path to your web config if I'm not mistaken and you are passing it null.

Try like this:

config = WebConfigurationManager.OpenWebConfiguration("~");

Also this might help you: How do you modify the web.config appSettings at runtime?

  • Thanks, but I get the following exception: "The virtual relative path '~' of the application is not allowed here." – VansFannel Aug 30 '18 at 09:18
  • Have you tried reading a specific value maybe? To make sure you are able to actually open the web config with null. Something like: config.AppSettings.Settings.Item("myKey").Value = txtmyKey.Text. If that works then try "Request.ApplicationPath" instead of '~' or null – João Cardoso Aug 30 '18 at 09:25
  • I'm developing a WinForm application. I'm opening it from that app, not from the ASP.NET application. – VansFannel Aug 30 '18 at 09:46
0

Well, first of all, you use web.config for a desktop application. It doesn't sound correct. Try to use app.config instead. Second, WebConfigurationManager.OpenWebConfiguration opens the Web-application configuration file

As for the topic, to get information from the config file try to use

var keys = ConfigurationManager.AppSettings.AllKeys
Elliott
  • 1
  • 1