是否可以在运行时在不同的Winform项目上使用不同的配置文件? [英] Is it possible to use different config file on different Winform Projects at runtime?

查看:124
本文介绍了是否可以在运行时在不同的Winform项目上使用不同的配置文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我有两个项目.让我们将其命名为Project A和ProjectB.

Basically I have two projects. Lets name it Project A and Project B.

两个项目都是WinForm应用程序.

Both projects are WinForm apps.

有时我会从项目A调用项目B中的表单.由于两个项目的连接字符串,业务层,服务和模型的实现方式不同,因此这些都在每个项目的app.config下设置,它们具有不同的功能.内容,当然.

There are times that I will call forms in Project B from Project A. Since both projects have different implementation of connection strings, business layer, services, and models, these are all set under app.config per project, which have different content, of course.

换句话说,项目A具有自己的app.config.项目B具有自己的app.config.

In other words, Project A has its own app.config. Project B has its own app.config.

现在,我想在运行时在winform/winapp项目上使用或更确切地说是app.config.这可能吗?

Now I want to use or rather switch app.config at runtime on my winform/winapp project. Is this possible?

问题在于,当我启动整个解决方案(将Project A设置为启动)时,将加载Project A的app.config.现在,我想每次调用Project B winforms时都使用Project B的app.config.

The problem is that when I launch the whole solution (Project A is set as startup), the app.config of Project A is loaded. Now I want to use the app.config of Project B everytime I will call Project B winforms.

感谢您可以提供的任何输入!

Thanks for any inputs you can provide!

推荐答案

如果您想在运行时更改配置文件,此代码对我来说效果很好,请首先创建一个类,您将调用该类来更改配置文件:

If you want to change your config file at run time, this code works perfectly for me, first create a class which you will call to change your config file:

public abstract class AppConfig : IDisposable
{
    public static AppConfig Change(string path)
    {
        return new ChangeAppConfig(path);
    }

    public abstract void Dispose();

    private class ChangeAppConfig : AppConfig
    {
        private readonly string oldConfig =
            AppDomain.CurrentDomain.GetData("APP_CONFIG_FILE").ToString();

        private bool disposedValue;

        public ChangeAppConfig(string path)
        {
            AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", path);
            ResetConfigMechanism();
        }

        public override void Dispose()
        {
            if (!disposedValue)
            {
                AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", oldConfig);
                ResetConfigMechanism();


                disposedValue = true;
            }
            GC.SuppressFinalize(this);
        }

        private static void ResetConfigMechanism()
        {
            typeof(ConfigurationManager)
                .GetField("s_initState", BindingFlags.NonPublic |
                                         BindingFlags.Static)
                .SetValue(null, 0);

            typeof(ConfigurationManager)
                .GetField("s_configSystem", BindingFlags.NonPublic |
                                            BindingFlags.Static)
                .SetValue(null, null);

            typeof(ConfigurationManager)
                .Assembly.GetTypes()
                .Where(x => x.FullName ==
                            "System.Configuration.ClientConfigPaths")
                .First()
                .GetField("s_current", BindingFlags.NonPublic |
                                       BindingFlags.Static)
                .SetValue(null, null);
        }
    }
}

然后在您的程序上调用this,您可以执行以下操作:

Then to call the this on your program, you can do this:

 using (AppConfig.Change(@"D:\app.config"))
        {
           //TODO:
        }

这篇关于是否可以在运行时在不同的Winform项目上使用不同的配置文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆