ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 索引器的妙用,读取配置文件

索引器的妙用,读取配置文件

原创 Linux操作系统 作者:iDotNetSpace 时间:2009-01-04 15:45:42 0 删除 编辑
public  class SysConfig
    {
        // Fields
        private static SysRead m_SysRead;

        // Methods
        public static string sValue(string sKey)
        {
            string sPath = AppDomain.CurrentDomain.BaseDirectory + @"Config";
            if (!Directory.Exists(sPath))
            {
                Directory.CreateDirectory(sPath);
            }
            string xmlFile = sPath + @"\Config.Xml";
            if (File.Exists(xmlFile))
            {
                m_SysRead = new SysRead(xmlFile);
                if (sKey == "Conn")
                {
                    return m_SysRead.sConnection;
                }
                return m_SysRead[sKey];
            }
            //MessageBox.Show("读配置文件失败", "提示", MessageBoxButtons.OK);
            return "";
        }
    }
    public  class SysRead
    {
        // Fields
        private XmlDocument m_xmlDoc;
        private string m_xmlFile;
        private static XmlNode m_xmlNode;

        // Methods
        public SysRead(string sXmlFile)
        {
            try
            {
                this.m_xmlFile = sXmlFile;
                this.m_xmlDoc = new XmlDocument();
                this.m_xmlDoc.Load(this.m_xmlFile);
                m_xmlNode = this.m_xmlDoc.SelectSingleNode("Config");
            }
            catch
            {
                //MessageBox.Show("配置文件中存在非法字符", "提示", MessageBoxButtons.OK);
            }
        }

        ~SysRead()
        {
            m_xmlNode = null;
            this.m_xmlDoc = null;
        }

        private static string getConnValue(string sKey)
        {
            try
            {
                string[] param = new string[2];
                param = sKey.Split(new char[] { '.' });
                XmlNodeList nodelist = m_xmlNode.ChildNodes;
                foreach (XmlElement xE in nodelist)
                {
                    if (xE.Name == param[0])
                    {
                        return xE.GetAttribute(param[1]);
                    }
                }
            }
            catch
            {
                return "";
            }
            return "";
        }

        // Properties
        public string this[string sKey]
        {
            get
            {
                return getConnValue(sKey);
            }
        }

        public string sConnection
        {
            get
            {
                return ("database=" + this["connect.DataBase"] + "; Server=" + this["connect.ServerIp"] + ";User ID=" + this["connect.Uid"] + ";Password=" + this["connect.Pw"] + ";Persist Security Info=True");
            }
        }
    }

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12639172/viewspace-526505/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-01-04

  • 博文量
    2376
  • 访问量
    5347126