Reputation: 45911
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?
Upvotes: 3
Views: 4795
Reputation: 1
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
Upvotes: 0
Reputation: 93003
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
).
Upvotes: 5
Reputation: 75
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?
Upvotes: 0