using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace CS
{
/// <summary>
/// 应用程序配置文件
/// </summary>
public class AppConfig
{
private bool updated = false;
private XmlDocument cfg;
private AppDomain app;
/// <summary>
/// 初始化当前应用程序配置
/// </summary>
public AppConfig()
{
cfg = new XmlDocument();
app = AppDomain.CurrentDomain;
this.Load();
}
/// <summary>
/// 加载配置文件
/// </summary>
public void Load()
{
this.updated = false;
cfg.Load(app.SetupInformation.ConfigurationFile);
}
/// <summary>
/// 获取或设置节点值
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public string this[string key]
{
get
{
return GetAppValue(key);
}
set
{
SetAppValue(key, value);
}
}
/// <summary>
/// 设置一个节点的值
/// </summary>
/// <param name="key"></param>
/// <param name="newValue"></param>
public void SetAppValue(string key, string newValue)
{
cfg.SelectSingleNode("/configuration/appSettings/add[@key='" + key + "']").Attributes["value"].Value = newValue;
this.updated = true;
}
/// <summary>
/// 保存修改到配置文件
/// </summary>
public void Save()
{
if (this.updated)
{
cfg.Save(app.SetupInformation.ConfigurationFile);
this.updated = false;
}
}
/// <summary>
/// 保存修改到配置文件,并重新加载新配置
/// </summary>
public void Update()
{
this.Save();
this.Load();
}
/// <summary>
/// 获取一个节点的值
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public string GetAppValue(string key)
{
return cfg.SelectSingleNode("/configuration/appSettings/add[@key='" + key + "']").Attributes["value"].Value;
}
}
}