using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Common.Config
{
///
/// 描述:配置信息工具
/// 作者:王昊昇
/// 时间:2010.5.17
///
public class ConfigurationUtil
{
public const string GLOBAL_CONFIGUATION_FILE_PATH = "app.config";
///
/// 判断配置文件是否存在
///
///
private static bool IsConfigFileExists()
{
#region
return System.IO.File.Exists(System.Windows.Forms.Application.StartupPath
+ "\\" + GLOBAL_CONFIGUATION_FILE_PATH);
#endregion
}
///
/// 判断指定配置项是否存在
///
/// 配置项的XPATH路径
///
public static bool IsConfigExists(string keyPath)
{
#region
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
try
{
doc.Load(System.Windows.Forms.Application.StartupPath
+ "\\" + GLOBAL_CONFIGUATION_FILE_PATH);
System.Xml.XmlNode node = doc.SelectSingleNode(keyPath);
if (node != null)
{
node = null;
doc = null;
return true;
}
}
catch
{
}
doc = null;
return false;
#endregion
}
///
/// 写入配置,配置不存在时将抛出异常
///
/// 配置项的XPATH路径
/// 值
public static void WriteConfig(string keyPath, string value)
{
#region
if (IsConfigExists(keyPath))
{
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(System.Windows.Forms.Application.StartupPath
+ "\\" + GLOBAL_CONFIGUATION_FILE_PATH);
System.Xml.XmlNode node = doc.SelectSingleNode(keyPath);
node.InnerText = value;
doc.Save(System.Windows.Forms.Application.StartupPath
+ "\\" + GLOBAL_CONFIGUATION_FILE_PATH);
}
else
{
throw new Exception("配置信息不存在");
}
#endregion
}
///
/// 读取指定的配置
///
/// 配置项的XPATH路径
///
public static string ReadConfig(string keyPath)
{
#region
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
try
{
doc.Load(System.Windows.Forms.Application.StartupPath
+ "\\" + GLOBAL_CONFIGUATION_FILE_PATH);
System.Xml.XmlNode node = doc.SelectSingleNode(keyPath);
if (node != null)
{
doc = null;
return node.InnerText;
}
}
catch
{
throw;
}
finally
{
doc = null;
}
throw new Exception("配置信息不存在");
#endregion
}
}
}