winform程序中如何修改app.config

原创|其它|编辑:郝浩|2010-02-23 10:36:01.000|阅读 2298 次

概述:在winform中使用程序读取和修改App.config里面的appSettings当中的Value值。

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

  用下面的方法可以操作应用程序文件夹下的配置文件:

  在winform中使用程序读取和修改App.config里面的appSettings当中的Value值

  这里我写成了两个方法,以供大家参考!

  一,命名空间


   using System;
  using System.Configuration;
  using System.Xml;

  二,方法


 //读取Value值
  public static string GetConfigString(string key)
  {
  //
  // TODO: 在此处添加构造函数逻辑
  //
  return ConfigurationSettings.AppSettings[key];
  }
  //写操作
  public static void SetValue(string AppKey,string AppValue)
  {
  XmlDocument xDoc = new XmlDocument();
  //获取可执行文件的路径和名称
  xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");
  XmlNode xNode;
  XmlElement xElem1;
  XmlElement xElem2;
  xNode = xDoc.SelectSingleNode("//appSettings");
  xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
  if ( xElem1 != null ) xElem1.SetAttribute("value",AppValue);
  else
  {
  xElem2 = xDoc.CreateElement("add");
  xElem2.SetAttribute("key",AppKey);
  xElem2.SetAttribute("value",AppValue);
  xNode.AppendChild(xElem2);
  }
  xDoc.Save(System.Windows.Forms.Application.ExecutablePath + ".config");
  }

  当Properties.Settings变量的范围"scope"设置为用户"user"时,通过上述方式读写操作并不是操作 了"test.exe.config"文件,实际操作的文件保存在"C:\Documents and Settings\Administrator\Local Settings\Application Data\"路径下面(注:Administrator是当前用户文件夹),文件名字叫"user.config"。点击工程Properties页面 中"设置"选项卡的"同步"按钮会提示这个路径。


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com

文章转载自:网络转载

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP