A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© aisini 金牌黑马   /  2014-8-20 14:37  /  1195 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using System.Text.RegularExpressions;

  6. namespace INIFileHandler
  7. {
  8.     /// <summary>
  9.     /// INI文件类
  10.     /// </summary>
  11.     public class INIFile
  12.     {
  13.         const string regexSection = "\\[[\\w\\s]*\\][^\\[]*";
  14.         const string regexSectionTitle = "^\\[([\\w\\s]*\\])$";
  15.         const string regexKeyValue = "^(\\w*\\s*)=(\\s*\\w*)$";
  16.         const string regexCommon = "^;.*$";

  17.         public string filepath;    //载入的文件内容。
  18.         public string content;     //以字符串的行保存载入文件的所有内容。
  19.         public List<Section> profileSections

  20.         public INIFile()
  21.         {
  22.             this.filepath = "";
  23.             this.content = "";
  24.         }

  25.         public INIFile(string filepath)
  26.         {
  27.             try
  28.             {
  29.                 if (!File.Exists(filepath)) return;
  30.                 this.filepath = filepath;
  31.             }
  32.             catch (Exception ex)
  33.             { }
  34.         }

  35.         public void Load(string filepath)
  36.         {
  37.             try
  38.             { content = filereader(filepath, Encoding.Default); }
  39.             catch (Exception)
  40.             { return; }
  41.             
  42.             foreach (Match section in Regex.Matches(content, regexSection))
  43.             {
  44.                 Section newSection = new Section();

  45.                 newSection.sectionTitle = Regex.Replace(Regex.Match(section.ToString(), regexSectionTitle).ToString(), regexSectionTitle, "$1").ToString().Trim();
  46.                 foreach (Match keyvalue in Regex.Matches(section.ToString(), regexKeyValue))
  47.                 {
  48.                     newSection.addKeyValue(new KeyValue(Regex.Replace(keyvalue.ToString(), regexKeyValue, "$1").ToString(), Regex.Replace(keyvalue.ToString(), regexKeyValue, "$2").ToString()));
  49.                 }

  50.                 this.profileSections.Add(newSection);
  51.             }
  52.         }

  53.         /// <summary>
  54.         /// 文件读入
  55.         /// </summary>
  56.         /// <param name="filename">文件名</param>
  57.         /// <param name="encoding">编码</param>
  58.         /// <returns>读入的文本内容</returns>
  59.         public string filereader(string filename, Encoding enc)
  60.         {
  61.             return "";
  62.         }

  63.         /// <summary>
  64.         /// 写入修改
  65.         /// </summary>
  66.         /// <param name="filename">文件名</param>
  67.         /// <param name="encoding">编码</param>
  68.         public void filewriter(string filename, Encoding enc)
  69.         {
  70.             
  71.         }
  72.     }

  73.     public class KeyValue
  74.     {
  75.         public string KeyName;
  76.         public string KeyValue;

  77.         public KeyValue()
  78.         {
  79.             KeyName = "";
  80.             KeyValue = "";
  81.         }

  82.         public KeyValue(string KeyName, string KeyValue)
  83.         {
  84.             this.KeyName = KeyName;
  85.             this.KeyValue = KeyValue;
  86.         }
  87.      
  88.     }

  89.     public class Section
  90.     {
  91.         public string sectionTitle;
  92.         public List<KeyValue> keyValues;
  93.         int Count;

  94.         public Section(string title, List<KeyValue> keyValues)
  95.         {
  96.             this.sectionTitle = title;
  97.             this.keyValues = keyValues;
  98.             this.Count = keyValues.Count;
  99.         }

  100.         public Section()
  101.         {
  102.             sectionTitle = "";
  103.             this.Count = 0;
  104.             keyValues = new List<KeyValue>() { };
  105.         }

  106.         /// <summary>
  107.         /// 添加键值对
  108.         /// </summary>
  109.         /// <param name="kv">KeyValue对象</param>
  110.         public void addKeyValue(KeyValue kv)
  111.         {
  112.             keyValues.Add(kv);
  113.         }
  114.     }

  115. }
复制代码


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马