国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

操作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

最大的網站源碼資源下載站,

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 夏河县| 望奎县| 景德镇市| 拜城县| 青川县| 新晃| 海南省| 彭州市| 大关县| 金寨县| 湖南省| 高陵县| 北安市| 阳高县| 延长县| 汤原县| 阳西县| 高碑店市| 蒲江县| 海兴县| 治县。| 会宁县| 广昌县| 疏勒县| 肃南| 朝阳区| 凤城市| 行唐县| 兰州市| 黄梅县| 绥芬河市| 噶尔县| 和龙市| 深水埗区| 新闻| 大田县| 凤山县| 陆川县| 瑞金市| 瑞金市| 都兰县|