WinForm操作config文件

往配置文件中保存配置信息

View Code
 1 public void SetConfigValue(string AppKey, string AppValue)
 2         {
 3             XmlDocument xDoc = new XmlDocument();
 4             xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");
 5             XmlNode xNode;
 6             XmlElement xElem1;
 7             XmlElement xElem2;
 8             xNode = xDoc.SelectSingleNode("//appSettings");
 9             xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
10             if (xElem1 != null)
11                 xElem1.SetAttribute("value", AppValue);
12             else//如果没有该节点,则添加
13             {
14                 xElem2 = xDoc.CreateElement("add");
15                 xElem2.SetAttribute("key", AppKey);
16                 xElem2.SetAttribute("value", AppValue);
17                 xNode.AppendChild(xElem2);
18             }
19             xDoc.Save(System.Windows.Forms.Application.ExecutablePath + ".config");
20         }

根据节点名称读取配置信息

View Code
 1 public string GetConfigValue(string AppKey)
 2         {
 3             XmlDocument xDoc = new XmlDocument();
 4             try
 5             {
 6                 xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");
 7                 XmlNode xNode = xDoc.SelectSingleNode("//appSettings");
 8                 XmlElement xElem = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
 9                 if (xElem != null)
10                     return xElem.GetAttribute("value");
11                 else
12                     return "";
13             }
14             catch (Exception ex)
15             {
16                 return ex.ToString();
17             }
18         }

 

转载于:https://www.cnblogs.com/Casker/archive/2013/03/28/2987182.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值