如何在WPF中指定应用程序的App.config文件的名称?

时间:2021-11-07 19:30:08

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

这非常令人沮丧...我可以为Windows窗体应用程序设置配置文件。考虑一下:

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?

但是,在WPF应用程序中,这似乎不起作用!如果我设置此值,则AppDomain.CurrentDomain.SetupInformation.ConfigurationFile属性的值是正确的,但在调试时对该配置文件的任何调用都不会产生任何结果。我需要在应用程序之间共享App.config中的WCF配置设置,因此这是我提出的解决方案。是否可以在WPF中动态设置配置文件的位置?

Help! Thanks!

2 个解决方案

#1


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.

注意:这将尝试在C:\ PathTo中打开名为app.exe.config的文件。这还要求文件存在于名为“app.exe”的同一路径中。 “app.exe”文件可以只是一个空文件。对于你的情况,我几乎会创建一个共享的“Config.dll”库来处理配置文件。

~md5sum~

#2


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.

这是在服务方面还是在客户端?如果在服务端,通常情况下服务在其自己的AppDomain中运行,那么如果您设置AppDomain.CurrentDomain.SetData(...),它将不会应用于服务配置。

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.

我不完全确定如何解决这个问题,但您应该能够通过实现自己的ServiceHost来控制服务的配置。

#1


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.

注意:这将尝试在C:\ PathTo中打开名为app.exe.config的文件。这还要求文件存在于名为“app.exe”的同一路径中。 “app.exe”文件可以只是一个空文件。对于你的情况,我几乎会创建一个共享的“Config.dll”库来处理配置文件。

~md5sum~

#2


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.

这是在服务方面还是在客户端?如果在服务端,通常情况下服务在其自己的AppDomain中运行,那么如果您设置AppDomain.CurrentDomain.SetData(...),它将不会应用于服务配置。

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.

我不完全确定如何解决这个问题,但您应该能够通过实现自己的ServiceHost来控制服务的配置。