using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Common.Config
{
    /// <summary>
    /// 描述:配置信息工具
    /// 作者:王昊昇
    /// 时间:2010.5.17
    /// </summary>
    public class ConfigurationUtil
    {
        public const string GLOBAL_CONFIGUATION_FILE_PATH = "app.config";
        /// <summary>
        /// 判断配置文件是否存在
        /// </summary>
        /// <returns></returns>
        private static bool IsConfigFileExists()
        {
            #region
            return System.IO.File.Exists(System.Windows.Forms.Application.StartupPath
                    + "\\" + GLOBAL_CONFIGUATION_FILE_PATH);
            #endregion
        }

        /// <summary>
        /// 判断指定配置项是否存在
        /// </summary>
        /// <param name="keyPath">配置项的XPATH路径</param>
        /// <returns></returns>
        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
        }

        /// <summary>
        /// 写入配置,配置不存在时将抛出异常
        /// </summary>
        /// <param name="keyPath">配置项的XPATH路径</param>
        /// <param name="value">值</param>
        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
        }

        /// <summary>
        /// 读取指定的配置
        /// </summary>
        /// <param name="keyPath">配置项的XPATH路径</param>
        /// <returns></returns>
        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
        }
    }
}