一个Visual Studio2010的bug….

今天发现一个VisualStudio2010的bug。是这样的,VS中Project属性有一项是关于Hosting Process的,如果我关闭这个选项,bug就出来了….

12-23-2011 11-28-26

大家知道,.NET程序有个App.config/web.config,我现在把它重定向到另外一个位置的配置文件:

   1: AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", newCfgFile);

这样重定向以后你用ConfigurationManager就会发现它会自动读取重定向的配置文件了。

为了演示,建立一个简单的WindowForms程序,代码:

   1: private void Form1_Load(object sender, EventArgs e)
   2: {
   3:     string newCfgFile = @"C:\2.cfg";
   4:  
   5:     AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", newCfgFile);
   6:  
   7:     this.textBox1.Text = ConfigurationManager.ConnectionStrings["SqlServices"].ConnectionString;
   8: }

程序App.config内容如下:

   1: <?xml version="1.0"?>
   2: <configuration>
   3:   <connectionStrings>
   4:     <add name="SqlServices" providerName="System.Data.SqlClient" connectionString="Persist Security Info=True;timeout=5;Data Source=127.0.0.1;Initial Catalog=abc;User ID=;Password="/>
   5:   </connectionStrings>
   6: </configuration>

2.cfg内容(重定向的):

   1: <?xml version="1.0"?>
   2: <configuration>
   3:   <connectionStrings>
   4:     <add name="SqlServices" providerName="System.Data.SqlClient" connectionString="Persist Security Info=True;timeout=5;Data Source=127.0.0.2;Initial Catalog=abc;User ID=;Password="/>
   5:   </connectionStrings>
   6: </configuration>

注意在debug模式下在VS2010中运行,如果我关闭”Enable Visual Studio Hosting Process”选项的话,取得的值就不对了。虽然重定向成功,但似乎ConfigurationManager还是读取老的配置文件。(如果不是在debug模式下在VS2010中运行,而是直接运行可执行文件*.exe,那就没有问题,所以可以断定是VS2010 debugger 的问题。)

PS: 不知道哪位大侠遇到过,似乎MSDN上有关Visual Studio Hosting Process的信息很少很少。

原文地址:https://www.cnblogs.com/Mainz/p/2299168.html