改变App.config中在运行时App、config

2023-09-07 01:47:51 作者:梦醒╯花已落

我在写一个测试的WinForms / C#/。NET 3.5应用程序,我们正在开发的系统,我们在需要运行时.config文件之间切换下跌,但这是谈到了将是一场噩梦。

下面的场景:在WinForms应用程序的目的是测试一个Web应用程序,分为5个子系统。测试过程的工作与子系统之间发送消息,以及这个过程是成功的各个子系统必须有自己的config文件。

有关我的测试应用程序我写了5个独立的配置文件。我希望我能够在运行时这5个文件之间进行切换,但问题是:我可以编程编辑应用程序.config文件了无数次,但这些变化只会影响一次。我一直在寻找了很长时间的形式来解决这个问题,但我还是没有成功。

我知道这个问题的定义可能有点混乱,但我真的AP preciate,如果有人帮我。

在此先感谢!

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

有些事我没有提到之前。原来,我们的系统各个子系统之间,WCF Web应用程序调用。对于性能测试的原因(我们使用蚂蚁4),我们不得不创建组件的本地副本,并从测试项目中引用它们。这听起来可能有点不对,但我们无法找到一个令人满意的方法来衡量一个远程应用程序的性能。

---最终更新---

C winform添加app.config文件

下面是我在做什么:

 公共无效UpdateAppSettings(字符串键,字符串值)
{
    的XmlDocument xmlDoc中= XMLDocument.load方法(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

    的foreach(在xmlDoc.DocumentElement的XmlElement项)
    {
        的foreach(在item.ChildNodes XmlNode的节点)
        {
            如果(node.Name ==键)
            {
                node.Attributes [0] .value的=值;
                打破;
            }
        }
    }

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

    System.Configuration.ConfigurationManager.RefreshSection(节/小节);
}
 

解决方案

更新

解决方案下面没有工作,因为XmlDocument的不处理,它似乎有些版本的.NET的正确,当给定的文件路径不关闭。该解决方案(在链接例如code)是打开一个流,这将做处置,并通过该流的保存功能。

有一个解决方案,在此显示。的http://www.devnewsgroups.net/group/microsoft.public.dotnet.xml/topic40736.aspx

低于旧的东西的

试试这个:

请注意,我改成了XPath,但它已经有一段时间,所以我可能已经得到了XPath的错误,但在任何情况下,你应该使用XPath和不会走路的树。正如你可以看到它是非常清晰的。

最重要的一点是使用语句,将配置(),我认为这是你的问题。

让我知道,好运气。

 公共无效UpdateAppSettings(字符串键,字符串值)
  {
    使用(XmlDocument的xmlDoc中=新的XmlDocument())
    {
      xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
      xmlDoc.DocumentElement.FirstChild.SelectSingleNode(后裔::+键).Attributes [0] .value的=值;
      xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
    }
    System.Configuration.ConfigurationManager.RefreshSection(节/小节);
  }
 

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.

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.

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.

Thanks in advance!

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

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 ---

Here's what I'm doing:

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

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.

A solution is shown here. http://www.devnewsgroups.net/group/microsoft.public.dotnet.xml/topic40736.aspx

Old stuff below

Try this:

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.

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

Let me know, good luck.

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