INI 文件读取专用类

来源:互联网 发布:大数据魔镜视频 编辑:程序博客网 时间:2024/05/17 00:18

一、INI文件读取专用

  1. public class IniFile
  2. {
  3.     /// <summary>
  4.     /// 读取INI文件专用类
  5.     /// </summary>
  6.     private Hashtable iniFile = new Hashtable();
  7.     public int Count { get { return iniFile.Count; } }
  8.     public string this[string IndexKey] { get { return iniFile[IndexKey].ToString(); } }
  9.     /// <summary>
  10.     /// 读取指定INI文件中的配置信息
  11.     /// </summary>
  12.     /// <param name="file">配置文件的完整路径名</param>
  13.     /// <param name="section">配置文件中的节名</param>
  14.     public IniFile(string file, string section)
  15.     {
  16.         string Section = "[" + section + "]";
  17.         LoadIniFile(file, Section);
  18.         //如果SpecialIniFilePath不为空,则从SpecialIniFilePath指定的文件中读取配置信息
  19.         if (iniFile.Count>0 && iniFile.Contains("SpecialIniFilePath"))
  20.         {
  21.             string path = this["SpecialIniFilePath"].Trim();
  22.             if (path != "") LoadIniFile(path, Section);
  23.         }
  24.     }
  25.     private void LoadIniFile(string filePath, string Section)
  26.     {
  27.         try
  28.         {
  29.             StreamReader sr = new StreamReader(filePath, System.Text.Encoding.Default);
  30.             string readLine = null;
  31.             bool readEnd = false;
  32.             string[] keyWord;
  33.             while ((readLine = sr.ReadLine()) != null)
  34.             {
  35.                 if (readLine == Section)   //是指定的节,则开始读取配置信息
  36.                 {
  37.                     while ((readLine = sr.ReadLine()) != null)
  38.                     {
  39.                         if (readLine != "")   //跳过空行
  40.                         {
  41.                             if (readLine.Substring(0, 1) == "[")   //是另一新节,则结束本次的读取
  42.                             {
  43.                                 readEnd = true;
  44.                                 break;
  45.                             }
  46.                             keyWord = readLine.Split('=');
  47.                             iniFile[keyWord[0].Trim()] = keyWord[1];
  48.                         }
  49.                     }
  50.                 }
  51.                 if (readEnd == truebreak;
  52.             }
  53.             sr.Close();
  54.         }
  55.         catch
  56.         {
  57.             iniFile.Clear();
  58.         }
  59.     }
  60. }

二、INI文件样例

[DBA]
SpecialIniFilePath=

ServerName=AppSrv
DatabaseName=AppDb
UserID=07AIQY6Vow5xph2075bjrz7uPX4WOG7767CKS08Tqy3vnf5013dlt19sRZ2UME67
UserPassword=066EMU2Zs91tld929E3fnv3yT80SKC67667GOW4Xu7zrjb49653hpx5wV6YQIA77
Timeout=90

三、使用方法

  1. IniFile ini = new IniFile(System.Windows.Forms.Application.StartupPath + @"/Appcfg.ini""Appcfg");
  2. if (ini.Count>0)
  3. {
  4.      _server = ini["ServerName"];
  5.      _database = ini["DatabaseName"];
  6.      _uid = ini["UserID"];
  7.      _uid = Crypt.RndDecrypt("uid", _uid);   //解密
  8.      _pwd = ini["UserPassword"];
  9.      _pwd = Crypt.RndDecrypt("pwd", _pwd);   //解密
  10.      _timeout = ini["Timeout"];
  11. }