获取ini配置文件各节的名称 For C#

来源:互联网 发布:淘宝车牌选号 编辑:程序博客网 时间:2024/05/17 04:50
        /// <summary>        /// 获取配置文件各节名称        /// </summary>        /// <param name="FileName">配置文件路径</param>        /// <returns>各节名称数组 如无节则返回null</returns>        /// <remarks>利用读取配置文件,采用正则表达式(\[.+?\])逐行分析</remarks>        public static string[] GetSectionNames(string FileName) {            string[] ret = null;            string iniContent = KEVIN.IO.FileBase.ReadFile(FileName);            System.Text.RegularExpressions.Regex re = new System.Text.RegularExpressions.Regex(@"\[.+?\]");            System.Text.RegularExpressions.Match ma = re.Match(iniContent);            string val = "";            while (ma.Success)            {                if (val != "") val += "|";                val += ma.Value.Replace("[", "").Replace("]", "");                ma = ma.NextMatch();            }            if (val != "") {                ret = val.Split('|');            }            return ret;        }