views:

359

answers:

2

This is very frustrating... I can set the Configuration File for a Windows Forms Application just fine. Consider this:

public static void Main(){
    AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", @"SharedAppConfig.config");
    //do other things
}

However, in a WPF application, this doesn't seem to work! If I set this value, the value of the AppDomain.CurrentDomain.SetupInformation.ConfigurationFile property is correct, but any calls to that configuration file while debugging yield no results. There are WCF configuration settings in an App.config that I need to share between application, so this is my proposed solution. Is it possible to dynamically set the location of my config file in WPF?

Help! Thanks!

A: 

Is this on the service side or the client side? If on the service side, it is often the case that the service is running in its own AppDomain, so that if you set AppDomain.CurrentDomain.SetData(...) it won't apply to the service configuration.

I'm not entirely sure how to get around this, but you should be able to control the service's configuration by implementing your own ServiceHost.

Karl
No, it's on the client side. I found that I can point my client applications' app.config files to the same external files (appConfig, binding, and client all pointing to SharedAppConfig.config, SharedBinding.config, and SharedClient.config using the "configSource" attribute). This seems to work in the mean time.
Pwninstein
+1  A: 

You should be able to do something along the lines of:

using System.Configuration;

public class TryThis
{
    Configuration config = ConfigurationManager.OpenExeConfiguration("C:\PathTo\app.exe");

    public static void Main()
    {
        // Get something from the config to test.
        string test = config.AppSettings.Settings["TestSetting"].Value;

        // Set a value in the config file.
        config.AppSettings.Settings["TestSetting"].Value = test;

        // Save the changes to disk.
        config.Save(ConfigurationSaveMode.Modified);
    }
}

NOTE: This will attempt to open a file named app.exe.config at C:\PathTo. This also REQUIRES that a file exists at the same path with the name "app.exe". The "app.exe" file can just be an empty file though. For your case I'd almost make a shared "Config.dll" library that would handle the config file.

~md5sum~

md5sum