ConfigurationManager not found
Make sure you have a reference to the assembly System.Configuration.dll
Make sure you have a reference to the assembly System.Configuration.dll
A quick test seems to show that these settings are only loaded at application startup. //edit the config file now. Console.ReadLine(); Console.WriteLine(ConfigurationManager.AppSettings[“ApplicationName”].ToString()); Console.WriteLine(“Press enter to redisplay”); //edit the config file again now. Console.ReadLine(); Console.WriteLine(ConfigurationManager.AppSettings[“ApplicationName”].ToString()); Console.ReadLine(); You’ll see that all outputs remain the same.
Although the using System.Configuration; command is automatically generated in the using section, for some reason the actual reference is not set. Go into add reference, .Net tab, and choose System.Configuration. ConfigurationManager will now be resolved. If you go to the project where the exact same setup works just fine and look at the references, you … Read more
http://mostlytech.blogspot.com/2007/11/programmatically-enumerate-wcf.html // Automagically find all client endpoints defined in app.config ClientSection clientSection = ConfigurationManager.GetSection(“system.serviceModel/client”) as ClientSection; ChannelEndpointElementCollection endpointCollection = clientSection.ElementInformation.Properties[string.Empty].Value as ChannelEndpointElementCollection; List<string> endpointNames = new List<string>(); foreach (ChannelEndpointElement endpointElement in endpointCollection) { endpointNames.Add(endpointElement.Name); } // use endpointNames somehow … Appears to work well.
Suffered from exact issue. Problem was because of NameValueSectionHandler in .config file. You should use AppSettingsSection instead: <configuration> <configSections> <section name=”DEV” type=”System.Configuration.AppSettingsSection” /> <section name=”TEST” type=”System.Configuration.AppSettingsSection” /> </configSections> <TEST> <add key=”key” value=”value1″ /> </TEST> <DEV> <add key=”key” value=”value2″ /> </DEV> </configuration> then in C# code: AppSettingsSection section = (AppSettingsSection)ConfigurationManager.GetSection(“TEST”); btw NameValueSectionHandler is not supported any … Read more
You need to reference System.Configuration.dll in your project as well as the “using” statement. Namespaces are (sometimes) “split” across assemblies. That means that types in a single namespace are actually in different assemblies. To determine which assembly a BCL or FCL type is in, look it up on MSDN. If you look at the help … Read more
It gets cached, on first access of a property, so it does not read from the physical file each time you ask for a value. This is why it is necessary to restart an Windows app (or Refresh the config) to get the latest value, and why an ASP.Net app automatically restarts when you edit … Read more
Apache Commons Configuration works great. It supports having the configuration stored in a wide range of formats on the backend including properties, XML, JNDI, and more. It is easy to use and to extend. To get the most flexibility out of it use a factory to get the configuration and just use the Configuration interface … Read more
I know I’m late 🙂 But this how i do it: public static void AddOrUpdateAppSettings(string key, string value) { try { var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); var settings = configFile.AppSettings.Settings; if (settings[key] == null) { settings.Add(key, value); } else { settings[key].Value = value; } configFile.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name); } catch (ConfigurationErrorsException) { Console.WriteLine(“Error writing app settings”); } } … Read more
WebConfigurationManger knows how to deal with configuration inheritance within a web application. As you know, there could be several web.config files in one applicaion – one in the root of the site and any number in subdirectories. You can pass path to the GetSection() method to get possible overridden config. If we’d looke at WebConfigurationManager … Read more