views:

901

answers:

3

Hi all,

I know I can open config files that are related to an assembly with the static ConfigurationManager.OpenExe(exePath) method but I just want to open a config that is not related to an assembly. Just a standard .NET config file...

Thanks for taking time to read this and I look forward to your answers!

Adam

+1  A: 

The config file is just an XML file, you can open it by:

private static XmlDocument loadConfigDocument()
{
    XmlDocument doc = null;
    try
    {
        doc = new XmlDocument();
        doc.Load(getConfigFilePath());
        return doc;
    }
    catch (System.IO.FileNotFoundException e)
    {
        throw new Exception("No configuration file found.", e);
        return null;
    }
    catch (Exception ex)
    {
        return null;
    }
}

and later retrieving values by:

    // retrieve appSettings node

    XmlNode node =  doc.SelectSingleNode("//appSettings");
Otávio Décio
Great point. I can then query it through link to XML. Thanks for that... funny how you dont think of things when your going a million miles an hour!Thank you,Adam
+1  A: 

If you want to dig a 'little' bit more in this subject, check out this series of articles in codeproject:

  1. Unraveling the Mysteries of .NET 2.0 Configuration

  2. Decoding the Mysteries of .NET 2.0 Configuration

  3. Cracking the Mysteries of .NET 2.0 Configuration
Ricky AH
+3  A: 

Hi logisrx,

the articles posted by Ricky are very good, but unfortunately they don't answer your question.

To solve your problem you should try this piece of code:

ExeConfigurationFileMap configMap = new ExeConfigurationFileMap();
configMap.ExeConfigFilename = @"d:\test\justAConfigFile.config.whateverYouLikeExtension";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
Oliver
This nailed it for me! Thanks!!
mr.b