程序启动了,但是我想随时调整config文件里面的配置参数,应该如何实现实时读取并生效呀~~~

解决方案 »

  1.   

    config文件是xml格式的,用 System.Xml名称空间下的类即可
      

  2.   

    using System;
    using System.Xml;namespace prjCx
    {
        /// <summary>
        /// Config 的摘要说明。
        /// </summary>
        public class Config
        {
            private String msFileName = null;        public String ConfigFile
            {
                get
                {
                    return this.msFileName;
                }
                set
                {
                    if (System.IO.File.Exists(value.Trim()))
                    {
                        this.msFileName = value.Trim();
                    }
                }
            }        public Config()
            {
                this.msFileName = String.Empty;
            }        public Config(String ConfigFile)
            {
                this.ConfigFile = ConfigFile.Trim();
            }        public bool ReadConfig(String ContentName, out String ContentValue)
            {
                bool bFlag = false;            ContentValue = String.Empty;            if (!System.IO.File.Exists(this.msFileName))
                {
                    return bFlag;
                }            try
                {
                    System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
                    xmlDoc.Load(this.msFileName);
                    System.Xml.XmlNode xmlNode = xmlDoc.SelectSingleNode(ContentName);
                    ContentValue = xmlNode.InnerText;                bFlag = true;
                }
                catch (XmlException xmle)
                {
                    System.Console.WriteLine(xmle.Message);
                }            return bFlag;
            }
            
            /// <summary>
            /// 读XML文件
            /// </summary>
            /// <param name="ContentName">节点名</param>
            /// <param name="PropertyName">属性名</param>
            /// <param name="PropertyValue">属性值(Ref)</param>
            /// <returns></returns>
            public bool ReadConfig(String ContentName, String PropertyName, out String PropertyValue)
            {
                bool bFlag = false;            PropertyValue = String.Empty;            if (!System.IO.File.Exists(this.msFileName))
                {
                    return bFlag;
                }            try
                {
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.Load(this.msFileName);                XmlNode xmlNode = xmlDoc.SelectSingleNode(ContentName);                XmlAttributeCollection xmlAttr = xmlNode.Attributes;                for (int i = 0; i < xmlAttr.Count; ++i)
                    {
                        if (xmlAttr.Item(i).Name == PropertyName)
                        {
                            PropertyValue = xmlAttr.Item(i).Value;
                            bFlag = true;
                            break;
                        }
                    }
                }
                catch (XmlException xmle)
                {
                    System.Console.WriteLine(xmle.Message);
                }            return bFlag;
            }        public bool WriteConfig(String ContentName, String ContentValue)
            {
                bool bFlag = false;            if (!System.IO.File.Exists(this.msFileName))
                {
                    return bFlag;
                }            try
                {
                    System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
                    xmlDoc.Load(this.msFileName);
                    System.Xml.XmlNode xmlNode = xmlDoc.SelectSingleNode(ContentName);
                    xmlNode.InnerText = ContentValue;                xmlDoc.Save(this.msFileName);                bFlag = true;
                }
                catch (XmlException xmle)
                {
                    System.Console.WriteLine(xmle.Message);
                }            return bFlag;
            }
            /// <summary>
            /// 写XML文件
            /// </summary>
            /// <param name="ContentName">节点名</param>
            /// <param name="PropertyName">属性名</param>
            /// <param name="PropertyValue">属性值</param>
            /// <returns></returns>
            public bool WriteConfig(String ContentName, String PropertyName, String PropertyValue)
            {
                bool bFlag = false;            if (!System.IO.File.Exists(this.msFileName))
                {
                    return bFlag;
                }            try
                {
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.Load(this.msFileName);                XmlNode xmlNode = xmlDoc.SelectSingleNode(ContentName);                XmlAttributeCollection xmlAttr = xmlNode.Attributes;                for (int i = 0; i < xmlAttr.Count; ++i)
                    {
                        if (xmlAttr.Item(i).Name == PropertyName)
                        {
                            xmlAttr.Item(i).Value = PropertyValue;
                            bFlag = true;
                            break;
                        }
                    }                xmlDoc.Save(this.msFileName);                bFlag = true;
                }
                catch (XmlException xmle)
                {
                    System.Console.WriteLine(xmle.Message);
                }            return bFlag;
            }
        }
    }