Esempio n. 1
0
        /// <summary>
        /// 逐行解析 Ini 配置文件字符串
        /// </summary>
        /// <param name="textReader"></param>
        /// <returns></returns>
        private static IniObject ParseIni(TextReader textReader)
        {
            IniObject  iniObj  = new IniObject();
            IniSection iniSect = null;

            while (textReader.Peek() != -1)
            {
                string line = textReader.ReadLine();
                if (string.IsNullOrEmpty(line) == false && Regices[2].IsMatch(line) == false)                       //跳过空行和注释
                {
                    Match match = Regices[0].Match(line);
                    if (match.Success)
                    {
                        iniSect = new IniSection(match.Groups[1].Value);
                        iniObj.Add(iniSect);
                        continue;
                    }

                    match = Regices[1].Match(line);
                    if (match.Success)
                    {
                        iniSect.Add(match.Groups[1].Value.Trim(), match.Groups[2].Value);
                    }
                }
            }
            return(iniObj);
        }
Esempio n. 2
0
        /// <summary>
        /// 从文件以指定编码创建一个新的 IniObject 实例对象
        /// </summary>
        /// <param name="fileUri">文件路径的 Uri 对象</param>
        /// <param name="encoding">文件编码</param>
        /// <returns>转换成功返回 IniObject 实例对象</returns>
        public static IniObject Load(Uri fileUri, Encoding encoding)
        {
            fileUri = ConvertAbsoluteUri(fileUri);

            if (!fileUri.IsFile)
            {
                throw new ArgumentException("所指定的必须是文件 URI", "fileUri");
            }

            //解释 Ini 文件
            using (TextReader textReader = new StreamReader(fileUri.GetComponents(UriComponents.Path, UriFormat.Unescaped), encoding))
            {
                IniObject iObj = ParseIni(textReader);
                iObj.Path = fileUri;
                return(iObj);
            }
        }