0

I was trying to utilise the ConfigurationManager to read a config file, but it seems to return, but the data is not the data in the .config file (in particular the ConfigurationManger is reporting no applicationSettings or appSettings and does have a connectionString value; though the configuration file itself has appSettings and no connectionString) I've tried both the OpenExeConfiguration and the OpenMappedExeConfiguration methods.

If I use XmlDocument to the same configuration file though I can see the correct nodes and appSettings.

I'm using just a basic .config file generated via a console application creation of settings.

It almost looks like the ConfigurationManager is not reading the data from the configuration file provided. The connection string provided in the configuration manager is "data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" which seems generic and not in any of the code or .config file

Configuration Manager Code

var configurationManager = ConfigurationManager.OpenExeConfiguration(@"D:\temp\my.config");

I've also tried with the following to deal with the addition of the .config; it is odd that if I just use "D:\temp\my" it errors with a message about invalid.

var configurationManager = ConfigurationManager.OpenExeConfiguration(@"D:\temp\my.config.config");

Configuration Manager with Mapping

ExeConfigurationFileMap map = new ExeConfigurationFileMap { ExeConfigFilename = @"D:\temp\my.config" };
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);

Xml Code

XmlDocument configurationDocument = new XmlDocument();
configurationDocument.Load(@"D:\temp\my.config");
ChrisHDog
  • 4,473
  • 8
  • 51
  • 77
  • 2
    Check this answer: https://stackoverflow.com/questions/1083927/configurationmanager-openexeconfiguration-loads-the-wrong-file – daniell89 Oct 10 '17 at 07:32
  • you want to read connectionstring, then you need to use, ConfigurationManager.Connectionstrings["key"] – Akshay Joy Oct 10 '17 at 07:32
  • 1
    The first connection you are receiving (SQLEXPRESS) is provided by machine.config. You should be able to access the ConnectionString directly using ConfigurationManager.ConnectionStrings["KEY"]. If you must use OpenExeConfiguration you must reference the exe ConfigurationManager.OpenExeConfiguration(@"D:\temp\my.exe"); – Greg Oct 10 '17 at 07:43
  • I've checked out the "loads the wrong file" link and the same thing occurs using the OpenMappedExeConfiguration – ChrisHDog Oct 10 '17 at 07:46
  • Clarification: I'm not after the connection string (there isn't any in the config file), I'm after the applicationSettings (there are two); it seems that it is loading the machine.config and not the supplied config file – ChrisHDog Oct 10 '17 at 07:46
  • For application settings use ConfigurationManager.AppSettings. – Greg Oct 10 '17 at 07:52
  • The AppSettings is empty, no results – ChrisHDog Oct 10 '17 at 07:57
  • It seems that it might be something with the naming of the file and the nodes in the file? That the section then has the node and if that doesn't match the myApplication.exe.config it has trouble? I think I might stick to the Xml method if that is the case as I want to process different config files that might not have that structure. – ChrisHDog Oct 10 '17 at 08:00
  • 1
    The configuration file must be named applicationName.exe.config for ConfigurationManager.AppSettings etc to work. If you are using custom nodes you have to define ConfigSection classes to map the values. Settings are accessed differently. MyProperties.settings are accessible using MyProperties.Default.NAME_OF_SETTING. – Greg Oct 10 '17 at 08:54

0 Answers0