/// <summary>
    /// 管理配置文件的对象管理
    /// </summary>
    public class ConfigurationManager
    {
        #region Properties

        static Configurations configInstance;
        static Configurations errorMessageInstance;

        //Help Message消息实例
        static Configurations helpMessageInstance;
        /// <summary>
        /// Return the help message instance
        /// </summary>
        public static Configurations HelpConfig
        {
            get
            {
                // Create the help message instance by default file when the dinstanc is null
                if (helpMessageInstance == null)
                {
                    return GetHelpMessage();
                }

                return helpMessageInstance;
            }
        }

        #endregion

        #region Static function to create and return config distance

        /// <summary>
        /// Get the config
        /// </summary>
        /// <returns></returns>
        static public Configurations GetConfig()
        {

            if (configInstance == null) {
                configInstance = new Configurations();
                try {
                    configInstance.ConfigFileName = "Config.xml";
                } catch (Exception e){
                    configInstance = null;
                    //throw;
                    MessageBox.Show(e.Message);
                }
            }

            return configInstance;
        }

        /// <summary>
        /// Get error message
        /// </summary>
        /// <returns></returns>
        static public Configurations GetErrorMessage()
        {
            if (errorMessageInstance == null) {
                errorMessageInstance = new Configurations();
                try {
                    errorMessageInstance.ConfigFileName = "ErrorMessage.xml";
                } catch (Exception e) {
                    errorMessageInstance = null;
                    //throw;
                    MessageBox.Show(e.Message);
                }
            }

            return errorMessageInstance;
        }

        /// <summary>
        ///  获取helpMessage配置实例
        /// </summary>
        /// <returns></returns>
        static public Configurations GetHelpMessage()
        {
            return GetHelpMessage("HelpMessage.xml");
        }

        /// <summary>
        ///  获取helpMessage配置实例
        /// </summary>
        /// <returns></returns>
        static public Configurations GetHelpMessage(string fileName)
        {
            // Create help message instance
            helpMessageInstance = new Configurations();
            try
            {
                helpMessageInstance.ConfigFileName = fileName;
            }
            catch (Exception e)
            {
                helpMessageInstance = null;
                MessageBox.Show(e.Message);
            }

            return helpMessageInstance;
        }

        #endregion
    }

    public class Configurations
    {
        private XmlDocument config;

        internal Configurations()
        {

        }

        /// <summary>
        /// 获取结点的值
        /// </summary>
        /// <param name="key">配置结点</param>
        /// <returns></returns>
        public string this[string key]
        {
            get
            {
                try {
                    string value = config.GetElementsByTagName(key)[0].InnerText.Trim();//config[key].InnerText.Trim();


                    return value;
                } catch {
                    throw new ArgumentException(key + " doesn't exist");
                }
            }
        }

        public string GetParent(string key)
        {
            try {
                string value = config.GetElementsByTagName(key)[0].ParentNode.Name.Trim();

                return value;
            } catch {
                throw new ArgumentException("The module name doesn't exist");
            }
        }


        /// <summary>
        /// 读取配置文件
        /// </summary>
        internal string ConfigFileName
        {
            set
            {
                //确定配置文件是否存在,没有则退出程序
                int delIndex;
                string path = Assembly.GetCallingAssembly().Location;
                delIndex = path.LastIndexOf(@"\");
                string desPath = path.Remove(delIndex);

                try {
                    string fileName = value;
                    FileInfo info = new FileInfo(desPath + @"\" + fileName);
                    if (!info.Exists) {
                        delIndex = path.IndexOf("bin");
                        if (delIndex < 0) {
                            throw new ArgumentException(fileName + " doesn't exist");
                        }

                        string pathSource = path.Remove(delIndex);

                        info = new FileInfo(pathSource + @"\Configuration\" + fileName);

                        if (!info.Exists) {

                            throw new ArgumentException(fileName + " doesn't exist");

                        }

                        File.Copy(pathSource + @"\Configuration\" + fileName, desPath + @"\" + fileName);

                    }
                    //XmlDataDocument doc = new XmlDataDocument();
                    //doc.Load(desPath + @"\" + fileName);

                    //config = doc.GetElementsByTagName("Configuration");

                    config = new XmlDataDocument();
                    config.Load(desPath + @"\" + fileName);

                    //config = doc.GetElementsByTagName("Configuration");


                } catch {
                    throw new ArgumentException("Can't open the file");
                }


            }
        }

    }

posted on 2009-03-23 14:39  rudyfung  阅读(276)  评论(0编辑  收藏  举报