/// <summary> /// 逐行解析 Ini 配置文件字符串 /// </summary> /// <param name="textReader"></param> /// <returns></returns> private static IniObject ParseIni(TextReader textReader) { IniObject iniObj = new IniObject(); string key = string.Empty; while (textReader.Peek() != -1) { string line = textReader.ReadLine(); if (!string.IsNullOrEmpty(line)) //跳过空行 { for (int i = 0; i < Regices.Length; i++) { Match match = Regices[i].Match(line); if (match.Success) { if (i == 0) { key = match.Groups[1].Value; iniObj.Add(new IniSection(key)); break; } else if (i == 1) { iniObj[key].Add(match.Groups[1].Value.Trim(), match.Groups[2].Value); } } } } } return(iniObj); }
/// <summary> /// 从文件以指定编码创建一个新的 IniObject 实例对象 /// </summary> /// <param name="fileUri">文件路径的 Uri 对象</param> /// <param name="encoding">文件编码</param> /// <returns>转换成功返回 IniObject 实例对象</returns> public static IniObject Load(Uri fileUri, Encoding encoding) { string tempPath = CheckinUri(fileUri); //解释 Ini 文件 using (TextReader textReader = new StreamReader(tempPath, encoding)) { IniObject iObj = ParseIni(textReader); iObj.Path = fileUri; return(iObj); } }