ConfigurationManager中重定向到另一文件 [英] Redirect ConfigurationManager to Another File

查看:214
本文介绍了ConfigurationManager中重定向到另一文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am looking to redirect the standard .Net ConfigurationManager class to another file; entirely. The path is determined at runtime so I can't use configSource or such (this is not a duplicate question - I have looked at the others).

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

I am essentially trying to duplicate what ASP.Net is doing behind the covers. Thus not only my classes should read from the new config file, but also any standard .Net stuff (the one I am specifically trying to get to work is the system.codeDom element).

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

I have cracked open Reflector and started looking at how ASP.Net does it - it's pretty hairy and completely undocumented. I was hoping someone else has reverse-engineered the process. Not necessarily looking for a complete solution (would be nice) but merely documentation.

推荐答案

我终于弄清楚了。有一个公共的文件的手段来做到这一点 - 但它隐藏在.NET Framework的深处。更改自己的配置文件需要思考(做不超过刷新ConfigurationManager中); ,但它有可能改变你通过公共的API创建一个AppDomain的配置文件

I finally figured it out. There is a public documented means to do this - but it's hidden away in the depths of the .Net framework. Changing your own config file requires reflection (to do no more than refresh the ConfigurationManager); but it is possible to alter the configuration file of an AppDomain that you create via public APIs.

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

No thanks to the Microsoft Connect feature I submitted, here is the code:

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);
    }
}



输出:

Output:

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

这篇关于ConfigurationManager中重定向到另一文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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