c#获取或修改配置文件
2013-07-13 11:00
369 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
namespace SZTMinorTool
{
/// <summary>
/// App.config配置类
/// </summary>
public class AppSettings
{
/// <summary>
/// 获取配置文件路径
/// </summary>
/// <returns></returns>
public static string AppConfig()
{
return System.IO.Path.Combine(Application.StartupPath, "App.config");//此处配置文件在程序目录下,或者设置为指定的配置文件路径
}
/// <summary>
/// 获取配置节点值
/// </summary>
/// <param name="appKey">节点key值</param>
/// <returns></returns>
public static string GetValue(string appKey)
{
XmlDocument xDoc = new XmlDocument();
try
{
xDoc.Load(AppSettings.AppConfig());
XmlNode xNode;
XmlElement xElem;
xNode = xDoc.SelectSingleNode("//appSettings"); //补充,需要在你的app.config 文件中增加一下,<appSetting> </appSetting>
xElem = (XmlElement)xNode.SelectSingleNode("//add[@key='" + appKey + "']");
if (xElem != null)
return xElem.GetAttribute("value");
else
return "";
}
catch (Exception)
{
return "";
}
}
/// <summary>
/// 设置配置节点值
/// </summary>
/// <param name="AppKey">key</param>
/// <param name="AppValue">value</param>
public static void SetValue(string AppKey, string AppValue)
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load(AppSettings.AppConfig());
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(AppSettings.AppConfig());
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
namespace SZTMinorTool
{
/// <summary>
/// App.config配置类
/// </summary>
public class AppSettings
{
/// <summary>
/// 获取配置文件路径
/// </summary>
/// <returns></returns>
public static string AppConfig()
{
return System.IO.Path.Combine(Application.StartupPath, "App.config");//此处配置文件在程序目录下,或者设置为指定的配置文件路径
}
/// <summary>
/// 获取配置节点值
/// </summary>
/// <param name="appKey">节点key值</param>
/// <returns></returns>
public static string GetValue(string appKey)
{
XmlDocument xDoc = new XmlDocument();
try
{
xDoc.Load(AppSettings.AppConfig());
XmlNode xNode;
XmlElement xElem;
xNode = xDoc.SelectSingleNode("//appSettings"); //补充,需要在你的app.config 文件中增加一下,<appSetting> </appSetting>
xElem = (XmlElement)xNode.SelectSingleNode("//add[@key='" + appKey + "']");
if (xElem != null)
return xElem.GetAttribute("value");
else
return "";
}
catch (Exception)
{
return "";
}
}
/// <summary>
/// 设置配置节点值
/// </summary>
/// <param name="AppKey">key</param>
/// <param name="AppValue">value</param>
public static void SetValue(string AppKey, string AppValue)
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load(AppSettings.AppConfig());
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(AppSettings.AppConfig());
}
}
}
相关文章推荐
- c#获取或修改配置文件
- C# 动态获取、修改、更新配置文件 实现思路
- C# 动态获取、修改、更新配置文件 实现思路
- C#获取web.config配置文件内容
- C# 读取配置文件和修改
- asp.net(c#)动态修改webservice的地址和端口(动态修改配置文件)
- [置顶] c# asp.net 修改webconfig文件 配置
- C# 修改保存 配置文件的两种方式以及区别
- C#获取web.config配置文件内容
- C# 配置文件读取与修改
- C#获取并修改文件扩展名的方法
- C#里如何读取和修改配置文件的值
- C#修改app.config配置文件信息
- C# 配置文件读取与修改
- C# 读取与修改配置文件
- 【C#】#103 动态修改App.config配置文件
- C#获取ftp文件最后修改时间
- C# app.config文件配置和修改
- Linux下获取系统的IP,子网掩码,网关,MAC和配置文件的修改
- Linux下获取系统的IP,子网掩码,网关,MAC和配置文件的修改