using System;
using System.Collections ;
using System.IO ;
using System.Diagnostics ;
using System.Security.Cryptography ;
namespace Bigeagle.Util
{
///
/// 配置文件类,最终类
///
public sealed class Ini
{
///
/// 配置文件路径
///
private string m_strIniFilePath ;
///
/// 是否已经初始化
///
private bool m_bIsLoad ;
///
/// 属性值数组
///
private ArrayList m_arrProperties ;
///
/// 配置文件路径
///
public string IniFilePath
{
get
{
return m_strIniFilePath ;
}
set
{
m_strIniFilePath = value ;
}
}//end method
///
/// 构造函数
///
public Ini()
{
m_strIniFilePath = "" ;
m_bIsLoad = false ;
m_arrProperties = new ArrayList() ;
}//end method
///
/// 重载构造函数
///
/// 配置文件路径
public Ini(string a_strIniFilePath)
{
m_strIniFilePath = a_strIniFilePath ;
m_bIsLoad = false ;
m_arrProperties = new ArrayList() ;
}//end method
///
/// 添加属性
///
/// 属性名称
/// 属性值
///
///
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
///
/// 设置属性值
///
/// 属性名称
/// 属性值
///
///
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
///
/// 删除属性
///
/// 属性名称
///
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
///
/// 取得指定属性值
///
/// 属性名称
///
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
///
/// 保存配置文件
///
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
///
/// 读取配置文件
///
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
///
/// 添加加密属性
///
/// 属性名称
/// 属性值
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("该属性已经存在")) ;
}
}
///
/// 设置属性值
///
/// 属性名称
/// 属性值
///
///
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
///
/// 属性类
///
public class Property
{
///
/// 属性名称
///
private string m_strName ;
///
/// 属性值
///
private string m_strValue ;
///
/// 存取属性名称
///
public string Name
{
get
{
return m_strName ;
}
set
{
m_strName = value ;
}
}//end method
///
/// 存取属性值
///
public string Value
{
get
{
return m_strValue ;
}
set
{
m_strValue = value ;
}
}//end method
///
/// 构造函数
///
public Property()
{
m_strName = "" ;
m_strValue = "" ;
}//end method
///
/// 重载构造函数
///
/// 属性名称
/// 属性值
public Property(string a_strName , string a_strValue)
{
m_strName = a_strName ;
m_strValue = a_strValue ;
}//end method
}
}//end namespace

