zoukankan      html  css  js  c++  java
  • 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(',');
            }
        }
        
    }
    
    
  • 相关阅读:
    Pycharm软件更换pip默认安装源为国内安装源
    电商网站名词item>SKU与SPU
    Linux通过端口号查看使用进程结束进程
    window系统下的pycharm对虚拟机中的Ubuntu系统操作MySQL数据库
    JAVA项目常用的异常处理情况总结
    公文流转系统(未完成)
    《程序员修炼之道》读后感(三)
    Java文件操作递归遍历文件目录
    Java Web初试连接数据库完成学生信息录入
    JavaJFrame窗口实现新课程添加
  • 原文地址:https://www.cnblogs.com/scotth/p/6373246.html
Copyright © 2011-2022 走看看