首页 > 开发 > 综合 > 正文

操作INI的class,转自OSLeague啊,作者:bigeagle:)

2024-07-21 02:24:30
字体:
来源:转载
供稿:网友
using system;
using system.collections ;
using system.io ;
using system.diagnostics ;
using system.security.cryptography ;

namespace bigeagle.util
{
    /// <summary>
    /// 配置文件类,最终类
    /// </summary>
    public sealed class ini
    {
        /// <summary>
        /// 配置文件路径
        /// </summary>
        private string m_strinifilepath ;

        /// <summary>
        /// 是否已经初始化
        /// </summary>
        private bool m_bisload ;

        /// <summary>
        /// 属性值数组
        /// </summary>
        private arraylist m_arrproperties ;


        /// <summary>
        /// 配置文件路径
        /// </summary>
        public string inifilepath
        {
            get
            {
                return m_strinifilepath ;
            }
            set
            {
                m_strinifilepath = value ;
            }
        }//end method

        

        /// <summary>
        /// 构造函数
        /// </summary>
        public ini()
        {
            m_strinifilepath = "" ;
            m_bisload = false ;
            m_arrproperties = new arraylist() ;
        }//end method

        /// <summary>
        /// 重载构造函数
        /// </summary>
        /// <param name="a_strinifilepath">配置文件路径</param>
        public ini(string a_strinifilepath)
        {
            m_strinifilepath = a_strinifilepath ;
            m_bisload = false ;
            m_arrproperties = new arraylist() ;
        }//end method


        /// <summary>
        /// 添加属性
        /// </summary>
        /// <param name="a_strname">属性名称</param>
        /// <param name="a_strvalue">属性值</param>
        /// <exception cref="exception"></exception>
        /// <remarks>如果已经有指定属性值,抛出异常</remarks>
        public void addproperty(string a_strname , string a_strvalue)
        {
            
            //检查是否已有该属性
            bool bexists = false ;
            for(int i = 0 ; i < m_arrproperties.count ; i ++)
            {
                property p = (property)m_arrproperties[i] ;
                if(p.name == a_strname)
                {
                    bexists = true ;
                    break ;
                }
            }
            if(!bexists)
            {
                m_arrproperties.add(new property(a_strname , a_strvalue)) ;
            }
            else
            {
                throw(new exception("该属性已经存在")) ;
            }
        }//end method

        /// <summary>
        /// 设置属性值
        /// </summary>
        /// <param name="a_strname">属性名称</param>
        /// <param name="a_strvalue">属性值</param>
        /// <exception cref="exception"></exception>
        /// <remarks>改变已有的属性值,如果没有找到指定属性,则抛出异常</remarks>
        public void setproperty(string a_strname , string a_strvalue)
        {
            
            bool bexists = false ;
            for(int i = 0 ; i < m_arrproperties.count ; i ++)
            {
                property p = (property)m_arrproperties[i] ;
                if(p.name == a_strname)
                {
                    ((property)m_arrproperties[i]).value = a_strvalue ;
                    bexists = true ;
                    break ;
                }
            }

            if(!bexists)
            {
                throw(new exception("未找到指定属性")) ;
            }
        }//end method

        /// <summary>
        /// 删除属性
        /// </summary>
        /// <param name="a_strname">属性名称</param>
        /// <remarks>如果没有找到属性则什么也不做</remarks>
        public void delproperty(string a_strname)
        {

            for(int i = 0 ; i < m_arrproperties.count ; i ++)
            {
                property p = (property)m_arrproperties[i] ;
                if(p.name == a_strname)
                {
                    m_arrproperties.remove(i) ;
                    break ;
                }
            }
        }//end method

        /// <summary>
        /// 取得指定属性值
        /// </summary>
        /// <param name="a_strname">属性名称</param>
        /// <returns>如果找不到该属性,返回""</returns>
        public string getproperty(string a_strname)
        {
#if debug
            debug.assert(a_strname.trim() != "" , "属性名称不正确" , "属性名称不能为空") ;
            debug.assert(m_bisload , "尚未初始化" , "没有打开配置文件") ;
#endif//debug

            for(int i = 0 ; i < m_arrproperties.count ; i ++)
            {
                property p = (property)m_arrproperties[i] ;
                if(p.name == a_strname)
                {
                    return p.value ;
                }
            }

            return "" ;

        }//end method

        /// <summary>
        /// 保存配置文件
        /// </summary>
        public void save()
        {
            textwriter tw = null ;
            try
            {
                //如果指定目录不存在则创建
                system.io.fileinfo fi = new system.io.fileinfo(m_strinifilepath) ;
                if(!fi.directory.exists)
                {
                    fi.directory.create() ;
                }
                
                tw = textwriter.synchronized(fi.createtext()) ;
                tw.writeline("#################################################################") ;
                tw.writeline("#") ;
                tw.writeline("# inifile create by bigeagle.util.ini") ;
                tw.writeline("#") ;
                tw.writeline("# author: [email protected]") ;
                tw.writeline("#") ;
                tw.writeline("################################################################") ;
                tw.writeline("") ;

                for(int i = 0 ; i < m_arrproperties.count ; i ++)
                {
                    property p = (property)m_arrproperties[i] ;
                    tw.writeline(p.name + " = " + p.value) ;
                }

                tw.close() ;
            }
            catch(exception e)
            {
#if debug
                console.writeline("写配置文件出错:" + e.message) ;
#endif
                throw(new exception("写配置文件出错:" + e.message)) ;
                
            }
            finally
            {
                if(tw != null)
                {
                    tw.close() ;
                }
            }

            
        }//end method

        /// <summary>
        /// 读取配置文件
        /// </summary>
        public void load()
        {
            textreader tr = null ;

            try
            {
                tr = textreader.synchronized(file.opentext(m_strinifilepath)) ;
                while(tr.peek() != -1)
                {
                    char ch = '=' ;
                    string str = tr.readline().replace(" " , "") ;

                    if(str.length > 0 && str.substring(0 , 1) != "#")
                    {
                        string[] temp = str.split(ch) ;
                        if(temp.length < 2)
                        {
                            throw(new exception("配置文件格式错误,每个属性行最少一个/"=/"号!")) ;

                        }
                        else
                        {
                            m_arrproperties.add(new property(temp[0] , str.substring(temp[0].length + 1))) ;
                        }
                    }
                    
                }

                tr.close() ;

                //已初始化
                this.m_bisload = true ;
            }
            catch(exception e)
            {
#if debug
                console.writeline("读取配置文件出错:" + e.message) ;
#endif//debug
                throw(new exception(e.message)) ;
            }
            finally
            {
                if(tr != null)
                {
                    tr.close() ;
                }
            }

        }//end method

        /// <summary>
        /// 添加加密属性
        /// </summary>
        /// <param name="a_strname">属性名称</param>
        /// <param name="a_strvalue">属性值</param>
        public void addsecurityproperty(string a_strname , string a_strvalue)
        {
            //检查是否已有该属性
            bool bexists = false ;
            for(int i = 0 ; i < m_arrproperties.count ; i ++)
            {
                property p = (property)m_arrproperties[i] ;
                if(p.name == a_strname)
                {
                    bexists = true ;
                    break ;
                }
            }
            if(!bexists)
            {
                m_arrproperties.add(new property(a_strname , bigeagle.util.cryptography.encryptmd5string(a_strvalue))) ;
            }
            else
            {
                throw(new exception("该属性已经存在")) ;
            }

        }

        /// <summary>
        /// 设置属性值
        /// </summary>
        /// <param name="a_strname">属性名称</param>
        /// <param name="a_strvalue">属性值</param>
        /// <exception cref="exception"></exception>
        /// <remarks>改变已有的属性值,如果没有找到指定属性,则抛出异常</remarks>
        public void setsecurityproperty(string a_strname , string a_strvalue)
        {
            
            bool bexists = false ;
            for(int i = 0 ; i < m_arrproperties.count ; i ++)
            {
                property p = (property)m_arrproperties[i] ;
                if(p.name == a_strname)
                {
                    ((property)m_arrproperties[i]).value = bigeagle.util.cryptography.encryptmd5string(a_strvalue) ;
                    bexists = true ;
                    break ;
                }
            }

            if(!bexists)
            {
                throw(new exception("未找到指定属性")) ;
            }
        }//end method


    }//end class


    /// <summary>
    /// 属性类
    /// </summary>
    public class property
    {
        /// <summary>
        /// 属性名称
        /// </summary>
        private string m_strname ;

        /// <summary>
        /// 属性值
        /// </summary>
        private string m_strvalue ;

        /// <summary>
        /// 存取属性名称
        /// </summary>
        public string name
        {
            get
            {
                return m_strname ;
            }
            set
            {
                m_strname = value ;
            }
        }//end method

        /// <summary>
        /// 存取属性值
        /// </summary>
        public string value
        {
            get
            {
                return m_strvalue ;
            }
            set
            {
                m_strvalue = value ;
            }
        }//end method

        /// <summary>
        /// 构造函数
        /// </summary>
        public property()
        {
            m_strname = "" ;
            m_strvalue = "" ;
        }//end method

        /// <summary>
        /// 重载构造函数
        /// </summary>
        /// <param name="a_strname">属性名称</param>
        /// <param name="a_strvalue">属性值</param>
        public property(string a_strname , string a_strvalue)
        {
            m_strname = a_strname ;
            m_strvalue = a_strvalue ;
        }//end method
    }
}//end namespace

最大的网站源码资源下载站,

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表