THE SETUP
Pretend you want to open an app.config or web.config file. OK, normally, that is easy. But, let’s say you mange a bunch of config files, and you want to write a widget that can open any of them and edit them programmatically. That means opening a config file NOT part of your EXE or website. Ah, now it gets more interesting. First you think,”I know, I can use ConfigurationManager.OpenExeConfiguration(path)!”
But then after a few hours of that madness driving me insane…I found a post that explains how that will never work. So then, how CAN it work? I mean, why didn’t they just give folks access to a ConfigurationManager.OpenConfiguration(path)? The world may never know.
GO AHEAD, TRY THIS AT HOME
To make this work (and really, I lived in denial about how convoluted this was…), I had to use the ConfigurationManager.OpenMappedEXEConfiguration. The method I used, from this post*, ended up looking something like:
using System.Configuration
.
.
.
public Configuration OpenConfig(string FileName)
{
Configuration cfg = null;
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = FileName;
try
{
cfg = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
}
catch
{
//whatever you need to do
}
return cfg; //where you put this depends on how you want to handle try/catch/finally
}
So there you have it. Good luck and happy multiple file configuring. Maybe I will continue this post if I hit more exciting goodies.
*Note, if you haven’t found out the dark secret…you can STILL see the answers to Experts-Exchange articles by scrolling past all their “pressure to join” garbage…all the way to the bottom! I think they know that blocking the answers will upset their user base greatly, as well as reduce their SEO. Joining is still handy, but not required.
THE SETUP
Pretend you want to open an app.config or web.config file. OK, normally, that is easy. But, let’s say you mange a bunch of config files, and you want to write a widget that can open any of them and edit them programmatically. That means opening a config file NOT part of your EXE or website. Ah, now it gets more interesting. First you think,”I know, I can use ConfigurationManager.OpenExeConfiguration(path)!”
But then after a few hours of that madness driving me insane…I found a post that explains how that will never work. So then, how CAN it work? I mean, why didn’t they just give folks access to a ConfigurationManager.OpenConfiguration(path)? The world may never know.
GO AHEAD, TRY THIS AT HOME
To make this work (and really, I lived in denial about how convoluted this was…), I had to use the ConfigurationManager.OpenMappedEXEConfiguration. The method I used ended up looking something like:
using System.Configuration . . . public Configuration OpenConfig(string FileName) { Configuration cfg = null; ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap(); fileMap.ExeConfigFilename = FileName; try { cfg = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None); } catch { //whatever you need to do } return cfg; //where you put this depends on how you want to handle try/catch/finally }