none
ConfigManager filename issue

    Question

  • My problem concerns using custom config files. When I load a config using 'System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration("example.config");' the manager saves to "example.config.config". How can I stop it appending an additional ".config" to the filename?
    Tuesday, May 02, 2006 4:46 PM

Answers

  • OpenExeConfiguration(string appName) is designed to work with application config files; a config file named specifically "appName.config".  The OpenExeConfiguration code checks for the existance of "appName" as well as "appName.config". 

    Because of this the following two lines will both open "MyApp.exe.config", but only if BOTH the exe *and* the config file exist.
      OpenExeConfiguration("MyApp.exe");
      OpenExeConfiguration("MyApp.exe.config");

    However, it doesn't check that MyApp.exe is actually a valid executable file.  So if this is truly something you need, you can fool the system by creating a file (ANY file, text, zero-byte, whatever) - called "example".  No extension, nothing.

    Now by calling
       OpenExeConfiguration("example");  
    ...or
       OpenExeConfiguration("example.config");

    The system will work the way you seem to expect.

    Wednesday, May 03, 2006 5:23 PM

All replies

  • I have been unable to duplicate this.  Are you calling (essentially) the following two lines of code...

    System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration("example.config");

    config.Save();

    In this case, Save doesn't create a new filename, it merely saves back to the file it previously opened.  Is this not what is happening for you?

    Wednesday, May 03, 2006 2:22 AM
  • There's a custom section bit between the open and save. You are correct that the open and save on there own do not produce the error, but with the custom section in place the filename goes bad:

    System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration("example.config");

    if (config.Sections["CustConfig"] == null)

    {

    CustConfigSection custSection = new CustConfigSection();

    config.Sections.Add("CustConfig", custSection);

    custSection = config.GetSection("CustConfig") as CustConfigSection;

    custSection.SectionInformation.ForceSave = true;

    }

    config.Save(ConfigurationSaveMode.Full);

    Wednesday, May 03, 2006 8:53 AM
  • OpenExeConfiguration(string appName) is designed to work with application config files; a config file named specifically "appName.config".  The OpenExeConfiguration code checks for the existance of "appName" as well as "appName.config". 

    Because of this the following two lines will both open "MyApp.exe.config", but only if BOTH the exe *and* the config file exist.
      OpenExeConfiguration("MyApp.exe");
      OpenExeConfiguration("MyApp.exe.config");

    However, it doesn't check that MyApp.exe is actually a valid executable file.  So if this is truly something you need, you can fool the system by creating a file (ANY file, text, zero-byte, whatever) - called "example".  No extension, nothing.

    Now by calling
       OpenExeConfiguration("example");  
    ...or
       OpenExeConfiguration("example.config");

    The system will work the way you seem to expect.

    Wednesday, May 03, 2006 5:23 PM
  • Excellent. What I actually need is OpenExeConfiguration("example.dll"). this opens and saves to example.dll.config.

    Thanks for your help

    Thursday, May 04, 2006 4:36 PM
  • To avoid this problem altogether, you can read in the web.config file as an XML file:

    using System.Xml;
    using System.Xml.XPath;

     

    doc.Load(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\..\\..\\..\\MyWebProject\\web.config");
    string value = doc.DocumentElement.SelectSingleNode("/configuration/appSettings/add[@key='MyKeyName']").Attributes["value"].Value;

    Monday, March 14, 2011 10:57 PM
  • To avoid this problem altogether, you can read in the web.config file as an XML file:

    using System.Xml;
    using System.Xml.XPath;

     

    doc.Load(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\..\\..\\..\\MyWebProject\\web.config");
    string value = doc.DocumentElement.SelectSingleNode("/configuration/appSettings/add[@key='MyKeyName']").Attributes["value"].Value;

    a bit brute force dont you think
    MickyD | http://mickyd.wordpress.com/ Help others by voting my post as 'Helpful' if you think it is so.
    Wednesday, January 25, 2012 3:22 AM
  • Como siempre nunca ponen bien los comentarios, voy a aclarar correctamente el código que debería de quedar.

    1. Se crean dos archivos y se agregan al directorio en directorio Debug.

    Fileconfig (sin extension) y Fileconfig.Config.

    Luego se crea la clase que va a extraer lo que se necesita.

    privatestringSearchConnectionString(stringpTipoBD)

            {

    var_config = ConfigurationManager.OpenExeConfiguration("FileConfig");

                  ConfigurationManager.OpenExeConfiguration("FileConfig.config");

    var_result = _config.ConnectionStrings.ConnectionStrings[""].ToString();

            

    if(_result != null)

                      {

    return_result;

                      }

    returnstring.Empty;

    y Listo Mágicamente funciona. Deberiamos de acostumbrarnos a hacer los comentarios y las soluciones correctamente para que sea más facil el poder ayudar a los demás y no poner más trabajo.

            }


    Gvega

    Sunday, July 21, 2013 4:52 AM