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

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: Bigeagle@163.net") ;
                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

时间: 2024-08-31 00:37:04

操作INI的class,转自OSLeague啊,作者:bigeagle:)的相关文章

不知道大家使用没使用我前几天写的操作INI文件的COM呢?我把代码贴出来。

这个是VB的代码,自己新建一个ActiveX DLL,然后改变工程名称和类名称,然后在类中输入如下内容即可. '//////////////////////////////'中文名称:INI文件操作类'英文名称:Blood_INI Class'作者:Blood'制作时间:2002.2.8'版本:1.0'版权所有 Blood 2002-2003'////////////////////////////// '申明变量Private strAppName As String             

操作INI文件的COM

这个是VB的代码,自己新建一个ActiveX DLL,然后改变工程名称和类名称,然后在类中输入如下内容即可. '//////////////////////////////'中文名称:INI文件操作类'英文名称:Blood_INI Class'作者:Blood'制作时间:2002.2.8'版本:1.0'版权所有 Blood 2002-2003'////////////////////////////// '申明变量Private strAppName As String 'INI小节名称Priv

介绍asp.net 操作INI文件的读写

 这篇文章主要介绍了asp.net 操作INI文件的读写,读写操作本地ini配置文件的方法,需要的朋友可以参考下  代码如下: using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebCon

C#操作ini文件

  我们首先在新建的项目,右键点击选择"添加"再选择"类",添加一个类,名为OperIni. 我们在类中引入WindowsAPI函数,GetPrivateProfileString与WritePrivateProfileString这两个函数就可以对Ini文件进行读写操作了. 引入上面两上函数后,我们就可以定义读.写.删除键.删除值的方法了. 4通过以上步骤,操作ini文件的类已经定义好了,现在我们在窗体的按键事件加入代码.通过断点调试,我们就可以清晰地看到ini文

关于C#操作INI文件的总结

原文:关于C#操作INI文件的总结          INI文件其实是一种具有特定结构的文本文件,它的构成分为三部分,结构如下: [Section1]key 1 = value2key 1 = value2--[Section2]key 1 = value1key 2 = value2-- 文件由若干个段落(section)组成,每个段落又分成若干个键(key)和值(value).Windows系统自带的Win32的API函数GetPrivateProfileString()和WritePriv

PHP中操作ini配置文件的方法_php实例

PHP操作ini配置文件 复制代码 代码如下: <?php//写ini文件function write_ini_file($assoc_arr, $path, $has_sections=FALSE){    $content = "";    if ($has_sections)    {        foreach ($assoc_arr as $key=>$elem)        {            $content .= "[".$ke

简单分析C#操作INI文件

C#语言还是比较常见的东西,这里我们主要介绍C#对INI文件操作,包括介绍对INI文件进行写操作等方面. C#对INI文件操作 对INI文件进行写操作,是通过组件button2的"Click"事件来实现的.这里有一点应该注意,当在调用 WritePrivateProfileString()对INI文件进行写操作的时候,如果此时在INI文件中存在和要写入的信息相同的段落名称和关键字,则将覆盖此INI信息.下面是button2组件的"Click"事件对应的代码清单: p

Delphi操作Ini文件

ini 文件操作记要(1): 使用 TIniFile unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Button2: TButton; Button3: TButton; Button4: TButton; But

asp.net操作ini文件示例_实用技巧

复制代码 代码如下: using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls; using Syste