【笔记】C#读取属性文件的类
2012-02-22 21:06
375 查看
View Code
C#读取属性文件的类。其中,属性文件的形式应该像这样
property=value
这种情况适用于GetPropertiesText
prope=value1,value2...
这种情况适用于GetPropertiesArray
给出一个测试的例子。
其中属性文件为test
内容为
test=value1,vaule2,value3
测试结果为
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace CTest { class PropertyFileOperator { private StreamReader sr = null; /// <summary> /// 构造函数 /// </summary> /// <param name="strFilePath">文件路径</param> public PropertyFileOperator(string strFilePath) { sr = new StreamReader(strFilePath); } /// <summary> /// 关闭文件流 /// </summary> public void Close() { sr.Close(); sr = null; } /// <summary> /// 根据键获得值字符串 /// </summary> /// <param name="strKey">键</param> /// <returns>值</returns> public string GetPropertiesText(string strKey) { string strResult = string.Empty; string str = string.Empty; 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; } /// <summary> /// 根据键获得值数组 /// </summary> /// <param name="strKey">键</param> /// <returns>值数组</returns> public string[] GetPropertiesArray(string strKey) { string strResult = string.Empty; string str = string.Empty; 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(','); } } }
C#读取属性文件的类。其中,属性文件的形式应该像这样
property=value
这种情况适用于GetPropertiesText
prope=value1,value2...
这种情况适用于GetPropertiesArray
给出一个测试的例子。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace CTest { class Program { static void Main(string[] args) { PropertyFileOperator pro = new PropertyFileOperator("test"); string[] s=pro.GetPropertiesArray("test"); foreach(string s1 in s) Console.WriteLine(s1); } } }
其中属性文件为test
内容为
test=value1,vaule2,value3
测试结果为
相关文章推荐
- C#IO 文件读取笔记一(File类和Directory类相关属性,FileInfo类和DirectoryInfo类)
- Android 笔记 读取style文件的属性
- 【个人学习笔记】xml 文件的属性读取
- 在C#中用Linq从属性文件中读取键值对Key-Value Pair
- C#学习笔记:从文件中读取数据
- Java学习笔记——JDBC读取properties属性文件
- Java学习笔记——JDBC读取properties属性文件
- 关于c# 读取dbf文件的数据库引擎驱动 (特别针对 shp图层的dbf属性文件)
- C#笔记 通用文件的读取之二——XML文件的读取与写入
- Java学习笔记——JDBC读取properties属性文件
- 【学习笔记】C#文件流联系|读取文件、追加Txt
- c#实现对MP4等文件属性信息的读取以及对txt文件内容的读取
- 【笔记】Spring配置文件中注册三种数据源及从属性文件中读取DB连接四要素
- 黑马程序员之C#学习笔记: xml文件读取
- C#笔记 通用文件的存取之二——XML文件的读取与写入
- C#IO 文件读取笔记二
- 黑马程序员之C#学习笔记:C#读取txt文件的方法
- Java学习笔记——JDBC读取properties属性文件
- C#学习笔记——FileStream读取文件示例
- C# 读取Properties属性文件