views:

675

answers:

4

To begin I have a .NET 2.0 console application. To meet a customer requirement, I need to use a configuration file which is in a different directory than my application in a read/write manner. This is in addition to the exe configuration which my app already has (ie appname.exe.config).

So to do this, I added a property Configuration which returns a Configuration object obtained from a call to OpenMappedExeConfiguration().

I'm trying to step back an re-evaluate this implementation. What I wrote seems really hackish. Is this even the right approach? I read through a lot of material on .net configuration files, but I'm still not sure exactly what I should do.

    class LocalEnvironment {

    ... stuff 

    static Configuration _cfg;

    internal static Configuration Configuration {
        get {

            if (_cfg == null) {
             var fm = new ExeConfigurationFileMap
                         {
                             ExeConfigFilename = Path.Combine(ApplicationInstallDirectory,
                                                              "config.xml")
                         };

                _cfg = ConfigurationManager.OpenMappedExeConfiguration(fm, ConfigurationUserLevel.None);
            }
            return _cfg;
        }
    }
}
A: 

I will do the same thing. No better solution I could found.

Henry Gao
+2  A: 

Why not just refer to the other file from the standard .config file?

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings file="..\RelativePathTo\MoreSettings.config">
  ...
  </appSettings>
</configuration>

MoreSettings.config

<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
    <add key="Something" value=""/>
</appSettings>
adrianm
The only problem with this approach is that I have to move all the app settings to the second file. Also, the relative path requirement may be an issue unless I update it during runtime.
sylvanaar
Completely agree with Adrian. Personally, I do not like this .config related configuration file with every assembly; creates a lot of mess. Make your own xml file, reference it in App.Config, and tell your customer to update that xml file; and well live a life. Any setting, any where in just one settings file.@Sylvanaar: I would not see this as a problem, because this is what the approach is intended towards; that is, not using app.config for settings but using some other (most likely xml) file for configuration. And everytime you change in app.config the application requires a restart.
KMan
Also, let me know if you require a working solution for this. I use it in my projects; its handy.
KMan
+1  A: 

This is what I do, and it's a heck of a lot simpler and more flexible. In this linked post there is example code - so it's even already written for you. :-)

http://stackoverflow.com/questions/1583673/net-suggestions-on-making-a-config-file-for-a-program/1583684#1583684

Ron

Ron Savage
You have a point - i guess I don't really need to use the framework's configuration system - I just started out using it - and had to adapt to a change in a customer requirement.
sylvanaar
I started out the same way not wanting to "re-invent the wheel" ... until I realized that their "wheel" is ridiculously over complicated. :-)
Ron Savage
A: 

There are valid reasons to do what you're doing. For example svcutil.exe (.net framework SDK tool) offers a /svcutilConfig switch which the user can use to cause a particular config file to be loaded as svcutil.exe's config file. This is needed so that the user can extend and customize the code generation process; these extensions are specified in config only.

The code you have is basically the same as what svcutil.exe does, and appears to be the best way to do this.

That said, it's worth stepping back and considering whether you really need to let the user replace the entire config file. As other posters have commented, perhaps it would be adequate just to give access to appSettings, which you can do more simply.

alexdej