在运行时更改 App.config [英] Changing App.config at Runtime

查看:49
本文介绍了在运行时更改 App.config的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我们正在开发的系统编写一个测试 WinForms/C#/.NET 3.5 应用程序,我们需要在运行时在 .config 文件之间切换,但结果证明这是一场噩梦.

I'm writing a test WinForms / C# / .NET 3.5 application for the system we're developing and we fell in the need to switch between .config files at runtime, but this is turning out to be a nightmare.

场景如下:WinForms 应用程序旨在测试一个 WebApp,分为 5 个子系统.测试过程处理子系统之间发送的消息,为了使该过程成功,每个子系统都必须拥有自己的 .config 文件.

Here's the scene: the WinForms application is aimed at testing a WebApp, divided into 5 subsystems. The test process works with messages being sent between the subsystems, and for this process to be successful each subsystem got to have its own .config file.

对于我的测试应用程序,我编写了 5 个单独的配置文件.我希望我能够在运行时在这 5 个文件之间切换,但问题是:我可以以编程方式多次编辑应用程序 .config 文件,但这些更改只会生效一次.我一直在寻找解决这个问题的表格很长时间,但我仍然没有成功.

For my Test Application I wrote 5 separate configuration files. I wish I was able to switch between these 5 files during runtime, but the problem is: I can programatically edit the application .config file numerous times, but these changes will only take effect once. I've been searching a long time for a form to address this problem but I still wasn't successful.

我知道问题定义可能有点混乱,但如果有人帮助我,我将不胜感激.

I know the problem definition may be a bit confusing but I would really appreciate it if someone helped me.

提前致谢!

--- 更新 01-06-10 ---

--- UPDATE 01-06-10 ---

有些事情我之前没有提到.最初,我们的系统是一个 Web 应用程序,每个子系统之间都有 WCF 调用.出于性能测试的原因(我们使用的是 ANTS 4),我们必须创建程序集的本地副本并从测试项目中引用它们.这听起来可能有点错误,但我们找不到一种令人满意的方法来衡量远程应用程序的性能.

There's something I didn't mention before. Originally, our system is a Web Application with WCF calls between each subsystem. For performance testing reasons (we're using ANTS 4), we had to create a local copy of the assemblies and reference them from the test project. It may sound a bit wrong, but we couldn't find a satisfying way to measure performance of a remote application.

--- 结束更新---

--- End Update ---

这是我正在做的事情:

public void UpdateAppSettings(string key, string value)
{
    XmlDocument xmlDoc = XmlDocument.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

    foreach (XmlElement item in xmlDoc.DocumentElement)
    {
        foreach (XmlNode node in item.ChildNodes)
        {
            if (node.Name == key)
            {
                node.Attributes[0].Value = value;
                break;
            }
        }
    }

    xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

    System.Configuration.ConfigurationManager.RefreshSection("section/subSection");    
}

推荐答案

UPDATE

下面的解决方案不起作用,因为 XmlDocument 没有处理,而且在给定文件路径时,某些版本的 .net 似乎没有正确关闭.解决方案(链接中的示例代码)是打开一个流,该流将执行处置并将该流传递给保存函数.

The solution below did not work because XmlDocument does not dispose and it seems some versions of .net do not close correctly when given a file path. The solution (example code in the link) is to open a stream which will do a dispose and pass that stream to the save function.

此处显示了解决方案.http://web-beta.archive.org/web/20150107004558/www.devnewsgroups.net/group/microsoft.public.dotnet.xml/topic40736.aspx

下面的旧东西

试试这个:

注意,我改成了 xpath,但已经有一段时间了,所以我可能把 xpath 弄错了,但无论如何你应该使用 xpath 而不是遍历树.如您所见,它更加清晰.

Note, I changed to xpath, but it has been a while so I might have gotten the xpath wrong, but in any case you should use xpath and not walk the tree. As you can see it is much clearer.

重点是 using 语句,它将 dispose(),我认为这是你的问题.

The important point is the using statement which will dispose(), which I think was your problem.

告诉我,祝你好运.

  public void UpdateAppSettings(string key, string value)
  {
    using (XmlDocument xmlDoc = new XmlDocument())
    {
      xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
      xmlDoc.DocumentElement.FirstChild.SelectSingleNode("descendant::"+key).Attributes[0].Value = value;
      xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
    }
    System.Configuration.ConfigurationManager.RefreshSection("section/subSection");
  }

这篇关于在运行时更改 App.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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