troubleshooting Question

reading .net config files using ConfigurationManager

Avatar of Mark_Buckingham
Mark_Buckingham asked on
Programming.NET Programming
6 Comments1 Solution4527 ViewsLast Modified:
I am trying to read a bespoke config file "deployment.config" as below.

I am able to open it ok and read the appropriate section into a ConfigurationSection this contains the correct information as proved by writing out the RawXml.

ConfigurationSection does ne no good as it doesn't expose the Settings of the section so i'm assuming i should be using ClientSettingsSection which does.

The problem is if i change line

ConfigurationSection section = config.GetSection("DeploymentConfiguration");


ClientSettingsSection section = config.GetSection("DeploymentConfiguration") as ClientSettingsSection;

i always get a null result. I would of thought this was a fairly common thing to do am i being stupid ?

<?xml version="1.0" encoding="utf-8" ?>
    <add key="MyAppConnectionstring"  value="TheConnectionstring"/>    
            ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
            fileMap.ExeConfigFilename = Environment.CurrentDirectory + @"\deployment.config";
            if (!File.Exists(fileMap.ExeConfigFilename))
                Console.WriteLine("Configuration file required :" + fileMap.ExeConfigFilename);
            Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);            
            ConfigurationSection section = config.GetSection("DeploymentConfiguration");
            Console.WriteLine (section.SectionInformation.GetRawXml());
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 6 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros