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(',');
        }
    }
    
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM