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(',');
}
}
}
CSharp读取配置文件的类(简单实现)的更多相关文章
- Asp.NetCore 读取配置文件帮助类
/// <summary> /// 读取配置文件信息 /// </summary> public class ConfigExtensions { public static ...
- spring读取配置文件PropertyPlaceholderConfigurer类的使用
这里主要介绍PropertyPlaceholderConfigurer这个类的使用,spring中的该类主要用来读取配置文件并将配置文件中的变量设置到上下文环境中,并进行赋值. 一.此处使用list标 ...
- PropertiesUtil 读取配置文件工具类
package org.konghao.basic.util; import java.io.FileInputStream; import java.io.FileNotFoundException ...
- ConfigUtil读取配置文件工具类
ConfigUtil package com.sso.util; import java.io.FileNotFoundException; import java.io.IOException; i ...
- java读取配置文件方法以及工具类
第一种方式 : java工具类读取配置文件工具类 只是案例代码 抓取异常以后的代码自己处理 import java.io.FileNotFoundException; import java.io. ...
- python读取配置文件&&简单封装
之前有做过把爬虫数据写到数据库中的练习,这次想把数据库信息抽离到一个ini配置文件中,这样做的好处在于可以在配置文件中添加多个数据库,方便切换(另外配置文件也可以添加诸如邮箱.url等信息) 1.co ...
- Asp.net Core 和类库读取配置文件信息
Asp.net Core 和类库读取配置文件信息 看干货请移步至.net core 读取配置文件公共类 首先开一个脑洞,Asp.net core 被使用这么长时间了,但是关于配置文件(json)的读取 ...
- 使用Properties去读取配置文件,并获得具体内容值
有时候,写了一个配置文件,需要知道读出来的内容对不对,我们需要测试一下,看看读出来的跟我们要的是不是一样.这里写了一个工具类,用来读取配置文件里面的内容. 一.使用Properties工具类来读取. ...
- SpringBoot 读取配置文件的值 赋给静态变量
需求:写了一个工具类,但是工具类中的一些变量需要放到配置文件中,而这个工具类中的变量与方法都是静态的,这个时候我需要一个办法将配置文件中的相关配置读取过来赋值给这些静态变量.找了一些文章,试了一些方法 ...
随机推荐
- 【puppeteer+Node.js安装环境】之WebStorm编辑器运行失败问题:Error: Cannot find module 'puppeteer'并且代码出不来“asnyc”标识以及有红色波浪线解决办法
现象一: module.js:557 throw err; ^ Error: Cannot find module 'puppeteer' at Function.Module._r ...
- HTTP POST请求数据提交格式(转)
FROM: http://bbs.125.la/thread-13743350-1-1.html HTTP/1.1 协议规定的 HTTP 请求方法有 OPTIONS.GET.HEAD.POST.PUT ...
- Memcache安装与使用
一.资源下载 安装memcached 之前必需要先安装 libevent 分别在libevent和memcached的官网下载安装包libevent-1.4.14b-stable.tar.gz和mem ...
- 为什么需要onRoute函数?
为什么需要onRoute函数? · Tencent/wepy Wiki https://github.com/Tencent/wepy/wiki/%E4%B8%BA%E4%BB%80%E4%B9%88 ...
- synchronized同步关键字
参考:http://blog.csdn.net/luoweifu/article/details/46613015 synchronized是Java中的关键字,是一种同步锁.它修饰的对象有以下几种: ...
- win7怎么设置打印机共享
一.设置好家庭组,让客户机加入家庭组 二.对服务机的打印机进行共享设置,如果保存不成功请在计算机服务那里打开防火墙 三.1.开启guest用户,具体操作:我的电脑右击---管理---本地用户和组--开 ...
- 谷歌postman插件的安装与使用
下载地址:http://pan.baidu.com/s/1kTh1g4B 安装方法: 1.下载并解压 2.解压后.打开谷歌浏览器.选择很多其它工具→扩展程序,如图 3.勾选开发人员模式 4.选择载入正 ...
- MV45AOZZ 销售订单增强点
[转自 http://blog.csdn.net/zhongguomao/article/details/6712580]choose the table VBAP or VBAK ( dependi ...
- 【ELK】Elasticsearch的备份和恢复
非原创,只是留作自己查询使用,转自http://keenwon.com/1393.html Elasticsearch的备份和恢复 备份 Elasticsearch的一大特点就是使用简单,api也比较 ...
- SQL Server分区表,能否按照多个列作为分区函数的分区依据(转载)
问: Hi, I have a table workcachedetail with 40 million rows which has 8 columns.We decided to partiti ...