操作INI的class,转自OSLeague啊,作者:bigeagle:) - 中国WEB开发者网络 (http://www.webasp.net) -- 技术教程 (http://www.webasp.net/article/) --- 操作INI的class,转自OSLeague啊,作者:bigeagle:) (http://www.webasp.net/article/4/3969.htm) |
| -- 作者:未知 -- 发布日期: 2003-07-12 |
| 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 |
| webasp.net |