C# 读取Properties属性文件_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > C# 读取Properties属性文件

C# 读取Properties属性文件

 2011/1/1 9:07:25  jonsion  http://jonsion.javaeye.com  我要评论(0)
  • 摘要:Properties属性文件操作类///<summary>///属性文件读取操作类///</summary>publicclassPropertyFileOperator{privateStreamReadersr=null;///<summary>///构造函数///</summary>///<paramname="strFilePath">文件路径</param>publicPropertyFileOperator
  • 标签:C# 文件
Properties属性文件操作类 
/// <summary>
 /// 属性文件读取操作类
 /// </summary>
 public 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.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;
     }
     /// <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.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(',');
     }
 }
发表评论
用户名: 匿名