C#下操作ini文件的封装

由于微软非常鼓励programmers使用注册表来代替ini文件,在.NET FRAMEWORK里只有操作注册表的封装,却没有对操作ini文件进行封装。下面的代码填补了这一项空白, 
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Collections;
using System.Collections.Specialized;
namespace Jerry.Huang
{
 /// <summary>
 /// 使用建议:虽然支持中文,但建议在ini文件中尽量避免使用中文,以避免在不同的操作系统产生不可预料的错误。
 /// 1.7 修改了int、bool的读写
 ///      增加了DateTime和Double类型的读写
 /// 原作者:陈省 修改:Jerry Huang
 /// 原始版本来源: http://hubdog.csdn.net/UpdateList/ul20030726.htm#Ini
 /// 一个模仿Delphi的TIniFile的类
 /// 修订:1.1 修正了对中文系统的支持。
 /// 1.2 增加了UpdateFile方法,实现了对Win9x的支持
 /// 1.3 增加了读写布尔,整数的操作
 /// 1.4 修正了写Ini虽然成功,但是会抛出异常的错误
 /// 1.5 ReadString返回的是Trim后的字符串
 /// 1.6 统一并扩大了读写缓冲区的大小
 /// </summary>
 public class IniFile
 {
  public string FileName; //INI文件名
  //声明读写INI文件的API函数
  [DllImport("kernel32")]
  private static extern bool WritePrivateProfileString(string section,string key,string val,string filePath);
  [DllImport("kernel32")]
  private static extern int GetPrivateProfileString(string section,string key,string def, byte[] retVal,int size,string filePath);
  /// <summary>
  /// 构造函数。
  /// 当指定的INI文件不存在时可选是否建立或抛出异常。
  /// </summary>
  /// <param name="IniFileName">文件名</param>
  /// <param name="ForceCreate">当文件不存在时是否建立</param>
  public IniFile(string IniFileName, bool ForceCreate)
  {
   // 判断文件是否存在
   FileInfo fileInfo=new FileInfo(IniFileName);
   //Todo:搞清枚举的用法
   if ((!fileInfo.Exists)) //|| (FileAttributes.Directory in fileInfo.Attributes))
    if (!ForceCreate)
    {
     throw(new ApplicationException("Ini文件不存在"));
    }
    else
    {
     fileInfo.Directory.Create();
     fileInfo.Create();
    }
   //必须是完全路径,不能是相对路径
   FileName = fileInfo.FullName;
  }
  

  #region 各种数据类型的读写
  /// <summary>
  /// 将string型值写入ini。
  /// </summary>
  /// <param name="Section">小节名</param>
  /// <param name="Ident">关键字</param>
  /// <param name="Value">要写入的值</param>
  public void WriteString(string Section,string Ident,string Value)
  {
   if (!WritePrivateProfileString(Section, Ident,Value,FileName))
   {
    // Todo:抛出自定义的异常
    throw(new ApplicationException("写Ini文件出错"));
   }
  }
  
  /// <summary>
  /// 从ini文件中读取string型值。
  /// 当读取失败时返回缺省值。
  /// </summary>
  /// <param name="Section">小节名</param>
  /// <param name="Ident">关键字</param>
  /// <param name="Default">缺省值</param>
  /// <returns>string型值</returns>
  public string ReadString(string Section,string Ident, string Default)
  {
   //StringBuilder Buffer = new StringBuilder(255);
   Byte[] Buffer=new Byte[65535];
   int bufLen=GetPrivateProfileString(Section,Ident,Default,Buffer, Buffer.GetUpperBound(0),FileName);
   //必须设定0(系统默认的代码页)的编码方式,否则无法支持中文
   string s=Encoding.GetEncoding(0).GetString(Buffer);
   s=s.Substring(0,bufLen);
   return s.Trim();
  }
  /// <summary>
  /// 从ini文件中读取int型值。
  /// 当读取失败时返回缺省值。
  /// </summary>
  /// <param name="Section">小节名</param>
  /// <param name="Ident">关键字</param>
  /// <param name="Default">缺省值</param>
  /// <returns>int型值</returns>
  public int ReadInteger(string Section, string Ident , int Default)
  {
   string intStr=ReadString(Section, Ident, Default.ToString());
   try
   {
    return Convert.ToInt32(intStr);
   }
   catch (Exception)
   {
    //Console.WriteLine(ex.Message);
    return Default;
   }
  }

  /// <summary>
  /// 将int型值写入ini
  /// </summary>
  /// <param name="Section">小节名</param>
  /// <param name="Ident">关键字</param>
  /// <param name="Value">要写入的值</param>
  public void WriteInteger(string Section,string Ident, int Value)
  {
   WriteString(Section, Ident, Value.ToString());
  }
  /// <summary>
  /// 从ini文件中读取bool型值。
  /// 当读取失败时返回缺省值。
  /// </summary>
  /// <param name="Section">小节名</param>
  /// <param name="Ident">关键字</param>
  /// <param name="Default">缺省值</param>
  /// <returns>bool型值</returns>
  public bool ReadBool(string Section, string Ident, bool Default)
  {
   try
   {
    return Convert.ToBoolean(ReadString(Section, Ident, Default.ToString() ));
   }
   catch (Exception)
   {
    //Console.WriteLine(ex.Message);
    return Default;
   }
  }
  /// <summary>
  /// 将bool型值写入ini
  /// </summary>
  /// <param name="Section">小节名</param>
  /// <param name="Ident">关键字</param>
  /// <param name="Value">要写入的值</param>
  public void WriteBool(string Section, string Ident , bool Value)
  {
   WriteString(Section, Ident, Value.ToString());
  }
  /// <summary>
  /// 将DateTime型值写入ini
  /// </summary>
  /// <param name="Section">小节名</param>
  /// <param name="Ident">关键字</param>
  /// <param name="Value">要写入的值</param>
  public void WriteDateTime(string Section, string Ident, DateTime Value)
  {
   WriteString(Section, Ident, Value.ToString());
  }
  /// <summary>
  /// 从ini文件中读取DateTime型值。
  /// 当读取失败时返回缺省值。
  /// </summary>
  /// <param name="Section">小节名</param>
  /// <param name="Ident">关键字</param>
  /// <param name="Default">缺省值</param>
  /// <returns>DateTime型值</returns>
  public DateTime ReadDateTime(string Section, string Ident, DateTime Default)
  {
   try
   {
    return Convert.ToDateTime(ReadString(Section, Ident, Default.ToString() ));
   }
   catch (Exception)
   {
    return Default;
   }
  }
  public void WriteDouble(string Section, string Ident, double Value)
  {
   WriteString(Section, Ident, Value.ToString());
  }
  public double ReadFloat(string Section, string Ident, double Default)
  {
   try
   {
    return Convert.ToDouble(ReadString(Section, Ident, Default.ToString() ));
   }
   catch (Exception)
   {
    return Default;
   }
  }
  #endregion
  //从Ini文件中,将指定的Section名称中的所有Ident添加到列表中
  public StringCollection ReadSection(string Section)
  {
   Byte[] Buffer=new Byte[16384];
   StringCollection Idents=new StringCollection();
   //Idents.Clear();
   int bufLen=GetPrivateProfileString(Section, null, null, Buffer, Buffer.GetUpperBound(0),
    FileName);
   //对Section进行解析
   GetStringsFromBuffer(Buffer, bufLen, Idents);
   return Idents;
  }
 
  private void GetStringsFromBuffer(Byte[] Buffer, int bufLen, StringCollection Strings)
  {
   Strings.Clear();
   if (bufLen!=0)
   {
    int start=0;
    for(int i=0; i < bufLen; i++)
    {
     if ((Buffer[i] == 0) && ((i-start)>0))
     {
      String s=Encoding.GetEncoding(0).GetString(Buffer, start, i-start);
      Strings.Add(s);
      start=i+1;
     }
    }
   }
  }
  //从Ini文件中,读取所有的Sections的名称
  public StringCollection ReadSections()
  {
   StringCollection SectionList=new StringCollection();
   //Note:必须得用Bytes来实现,StringBuilder只能取到第一个Section
   byte[] Buffer = new byte[65535];
   int bufLen=0;
   bufLen = GetPrivateProfileString(null, null, null,Buffer,
    Buffer.GetUpperBound(0), FileName);
   GetStringsFromBuffer(Buffer, bufLen, SectionList);
   return SectionList;
  }
  //读取指定的Section的所有Value到列表中
  public NameValueCollection ReadSectionValues(string Section)
  {
   NameValueCollection Values=new NameValueCollection();
   StringCollection KeyList=ReadSection(Section);
   Values.Clear();
   foreach(string key in KeyList)
   {
    Values.Add(key, ReadString(Section, key, ""));
   }
   return Values;
  }
  //清除某个Section
  public void EraseSection(string Section)
  {
   //
   if (!WritePrivateProfileString(Section, null, null, FileName))
   {
    throw(new ApplicationException("无法清除Ini文件中的Section"));
   }
  }
  //删除某个Section下的键
  public void DeleteKey(string Section, string Ident)
  {
   WritePrivateProfileString(Section, Ident, null, FileName);
  }
  //Note:对于Win9X,来说需要实现UpdateFile方法将缓冲中的数据写入文件
  //在Win NT, 2000和XP上,都是直接写文件,没有缓冲,所以,无须实现UpdateFile
  //执行完对Ini文件的修改之后,应该调用本方法更新缓冲区。
  public void UpdateFile()
  {
   WritePrivateProfileString(null, null, null, FileName);
  }
  /// <summary>
  /// 检查某个Section是否存在。
  /// </summary>
  /// <param name="Section">小节名</param>
  /// <returns>存在返回true,否则为false。</returns>
  public bool SectionExists(string Section)
  {
   StringCollection Sections=this.ReadSections();
   return Sections.IndexOf(Section)>-1;
  }
  /// <summary>
  /// 检查某个Section的某个键值是否存在。
  /// </summary>
  /// <param name="Section">小节名</param>
  /// <param name="Ident">关键字</param>
  /// <returns>存在返回true,否则为false。</returns>
  public bool ValueExists(string Section, string Ident)
  {
   //
   StringCollection Idents=ReadSection(Section);
   return Idents.IndexOf(Ident)>-1;
  }
  //确保资源的释放
  ~IniFile()
  {
   UpdateFile();
  }
 }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值