基本信息
源码名称:C#写的INI操作库LightINI-master
源码大小:6.57KB
文件格式:.zip
开发语言:C#
更新时间:2019-06-09
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍

using System;
using System.Collections.Generic;
using System.Linq;

namespace LightINI
{
    public class IniFile : IEnumerable<IniSection>
    {
        /// <summary>
        /// 注释字符
        /// </summary>
        public const string CommetChars = ";#";

        /// <summary>
        /// 所有节点
        /// </summary>
        private readonly HashSet<IniSection> Sections;

        public IniFile()
        {
            Sections = new HashSet<IniSection>();
        }

        public IniSection this[string key]
        {
            get { return Get(key); }
        }

        public IniSection Get(string sectionName)
        {
            return Sections.FirstOrDefault(s => s.Name.Equals(sectionName, StringComparison.OrdinalIgnoreCase));
        }

        public IniSection Get(string sectionName, bool append)
        {
            var section = Get(sectionName);

            if (append)
            {
                section = new IniSection { Name = sectionName };
                Sections.Add(section);
            }

            return section;
        }

        public void Set(string sectionName, string key, object value)
        {
            var section = Get(sectionName);
            if (section == null)
            {
                section = new IniSection
                {
                    Name = sectionName
                };
                Sections.Add(section);
            }
            section.Set(key, value);
        }

        public string GetValue(string sectionName, string key)
        {
            var section = Get(sectionName);
            return section == null ? null : section.GetValue(key);
        }

        /// <summary>
        /// 是否存在指定节点
        /// </summary>
        /// <param name="sectionName"></param>
        /// <returns></returns>
        public bool Has(string sectionName)
        {
            return Sections.Any(s => s.Name.Equals(sectionName, StringComparison.OrdinalIgnoreCase));
        }

        /// <summary>
        /// 是否存在指定节点,并且节点下存在指定的项
        /// </summary>
        /// <param name="sectionName"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool Has(string sectionName, string key)
        {
            var section = Get(sectionName);
            return section != null && section.Has(key);
        }

        /// <summary>
        /// 将内容输出为INI文件结构
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return string.Join("", Sections.Select(i => i.ToString()));
        }
        /// <summary>
        /// 从文本内容加载INI文件
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static IniFile LoadText(string text)
        {
            var ini = new IniFile();
            if (!string.IsNullOrWhiteSpace(text))
            {
                var filerows = text.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
                    .Select(row => row.Trim())
                    .ToArray();

                IniSection section = null;

                foreach (var row in filerows)
                {
                    if (string.IsNullOrWhiteSpace(row) || CommetChars.Contains(row[0]))
                    {
                        continue;
                    }
                    if (row.StartsWith("["))
                    {
                        section = new IniSection
                        {
                            Name = row.Trim("[]".ToCharArray())
                        };
                        ini.Sections.Add(section);
                        continue;
                    }
                    if (section == null)
                    {
                        continue;
                    }
                    var temp = row.Split('=');
                    var key = temp[0].Trim();
                    string val = null;
                    if (temp.Length == 2)
                    {
                        val = temp[1].Trim();
                    }
                    section.Set(key, val);
                }
            }

            return ini;
        }

        public IEnumerator<IniSection> GetEnumerator()
        {
            return Sections.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return Sections.GetEnumerator();
        }
    }
}