ConfigurationManager中重定向到另一文件(Redirect Configurati

2019-07-30 23:38发布

我期待标准的.NET类ConfigurationManager中重定向到另一个文件; 完全 。 路径是在运行时确定的,所以我不能使用configSource或这样的(这不是一个重复的问题-我已经看了看其他人)。

我基本上是试图复制什么ASP.Net是做封面的背后。 因此,不仅是我的课应从新的配置文件读取,而且任何标准的.NET的东西(一个我特别想获得工作是system.codeDom元素)。

我已经裂了开来反射并开始寻找在ASP.Net是怎么做的 - 它的晦涩完全无证。 我希望其他人逆向工程的过程。 不一定要找一个完整的解决方案(将是不错),但仅仅是文档

Answer 1:

我终于弄明白了。 有记录的方式做到这一点公共-但它隐藏在.NET Framework的深处。 更改自己的配置文件需要反思(做不超过刷新ConfigurationManager中); 但它可以改变你通过公共的API创建一个AppDomain的配置文件。

没有感谢微软连接功能,我提交的,这里是代码:

class Program
{
    static void Main(string[] args)
    {
        // Setup information for the new appdomain.
        AppDomainSetup setup = new AppDomainSetup();
        setup.ConfigurationFile = "C:\\my.config";

        // Create the new appdomain with the new config.
        AppDomain d2 = AppDomain.CreateDomain("customDomain", AppDomain.CurrentDomain.Evidence, setup);

        // Call the write config method in that appdomain.
        CrossAppDomainDelegate del = new CrossAppDomainDelegate(WriteConfig);
        d2.DoCallBack(del);

        // Call the write config in our appdomain.
        WriteConfig();

        Console.ReadLine();
    }

    static void WriteConfig()
    {
        // Get our config file.
        Configuration c = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

        // Write it out.
        Console.WriteLine("{0}: {1}", AppDomain.CurrentDomain.FriendlyName, c.FilePath);
    }
}

输出:

customDomain: C:\my.config
InternalConfigTest.vshost.exe: D:\Profile\...\InternalConfigTest.vshost.exe.config


文章来源: Redirect ConfigurationManager to Another File