CSharp读取配置文件的类(简单实现)

Reinventing the wheel 系列

CSharp 读取配置文件的类 简单实现(注意没有写)

本人对CS 不是很熟,库也不熟,所以到网上找个实现,并自己添加了点异常。如果只是读取信息,足够了。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Windows.Forms;
using System.IO;//StringReader


namespace CSharpTest
{
    //Properties属性文件操作类
    /// <summary>
    /// 属性文件读取操作类
    /// </summary>
    public class PropertyFileOperator
    {
        private StreamReader sr = null;

        private Boolean bIsInit = false;


        /// 构造函数
        /// <param name="strFilePath">文件路径</param>
        public PropertyFileOperator(string strFilePath)
        {
            try
            {
                sr = new StreamReader(strFilePath);
            }
            catch (Exception e)//can not find , exception
            {
                bIsInit = false;
                MessageBox.Show(e.Message,"ERROR:failed to read Property File 读取配置文件失败");
                return;
            }
            bIsInit = true;
        }

        public Boolean IsInit()
        {
            return bIsInit;
        }



        /// 关闭文件流
        public void Close()
        {
            sr.Close();
            sr = null;
        }


        /// 根据键获得值字符串
       
        /// <param name="strKey">键</param>
        /// <returns>值</returns>
        public string GetPropertiesText(string strKey)
        {
            string strResult = string.Empty;
            string str = string.Empty;
            sr.BaseStream.Seek(0, SeekOrigin.End);
            sr.BaseStream.Seek(0, SeekOrigin.Begin);
            while ((str = sr.ReadLine()) != null)
            {
                //int index = str.IndexOf('#');
                //bool ret = str.Substring(0, index).Equals("#");

                //int len = str.Length;  //len==0
                if (str.IndexOf('#') == 0 || str.CompareTo("") == 0)//comment
                    continue;


                if (str.IndexOf('=') >= 0)
                {
                    if (str.Substring(0, str.IndexOf('=')).Equals(strKey))
                    {
                        strResult = str.Substring(str.IndexOf('=') + 1);
                        if (strResult.IndexOf('#') >= 0)
                        {
                            strResult = strResult.Substring(0, strResult.IndexOf('#'));
                        }

                        break;
                    }

                }
                else if (str.IndexOf(':') >= 0)
                {
                    if (str.Substring(0, str.IndexOf(':')).Equals(strKey))
                    {
                        strResult = str.Substring(str.IndexOf(':') + 1);
                        if (strResult.IndexOf('#') >= 0)
                        {
                            strResult = strResult.Substring(0, strResult.IndexOf('#'));
                        }
                        break;
                    }

                }
               
            }
            return strResult;
        }

        /// 根据键获得值数组
        /// <param name="strKey">键</param>
        /// <returns>值数组</returns>
        public string[] GetPropertiesArray(string strKey)
        {
            string strResult = string.Empty;
            string str = string.Empty;
            sr.BaseStream.Seek(0, SeekOrigin.End);
            sr.BaseStream.Seek(0, SeekOrigin.Begin);
            while ((str = sr.ReadLine()) != null)
            {
                if (str.Substring(0, str.IndexOf('=')).Equals(strKey))
                {
                    strResult = str.Substring(str.IndexOf('=') + 1);
                    break;
                }
            }
            return strResult.Split(',');
        }
    }
    
}

原文地址:https://www.cnblogs.com/scotth/p/6373246.html