Exemple #1
0
        /// <summary>
        /// 从文件中读取配置信息。
        /// </summary>
        /// <param name="file">需要加载的文件</param>
        /// <returns>返回true表示加载成功,否则加载失败。</returns>
        public bool Load(string file = "config.ini")
        {
            if (!File.Exists(file))
            {
                File.Create(file).Close();
            }

            filepath     = file;
            sectionNames = new List <string>();
            string currentSection = "Others";

            map = new Dictionary <string, ConfigItem>();
            string[] lines  = File.ReadAllLines(file, FileEncoding);
            int      lineNo = -1;

            foreach (string line in lines)
            {
                lineNo++;

                ConfigItem ci = ConfigItem.Parse(line);
                if (ci == null)
                {
                    continue;
                }

                if (ci.IsSectionName)
                {
                    currentSection = ci.SectionName;
                    continue;
                }

                if (ci.Key == null)
                {
                    continue;
                }

                ci.LineNo      = lineNo;
                ci.SectionName = currentSection;

                // 如果值不存在则添加
                if (!map.Keys.Contains(ci.FullKey))
                {
                    map.Add(ci.FullKey, ci);
                    if (!sectionNames.Contains(currentSection))
                    {
                        sectionNames.Add(currentSection);
                    }
                }
            }

            return(true);
        }