日期:2013-08-19  浏览次数:20548 次

 

using Microsoft.VisualBasic.CompilerServices;
using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Text;

namespace INIManage
{
 /// <summary>
 /// INIManage 的摘要说明
 /// 在http://www.allapi.net/ 上发现了一个VB.NET的INI文件操作类,下载了看了看,顺手改成了C#版的
 /// 你可以把它编译成dll在winform或webform中引用,也可以直接把代码拷到项目中使用
 /// 我没有进行逐项测试,所以可能有不对的地方,请酌情修改
 /// --------------丛兴滋(cncxz) 2005-08-23
 /// </summary>
 public class INIManage
 {

  #region" 引入相关dll "

  [DllImport("KERNEL32.DLL", EntryPoint="GetPrivateProfileIntA", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi, ExactSpelling=true)]
  private static extern int GetPrivateProfileInt(string lpApplicationName, string lpKeyName, int nDefault, string lpFileName);

  [DllImport("KERNEL32.DLL", EntryPoint="GetPrivateProfileSectionsNamesA", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi, ExactSpelling=true)]
  private static extern int GetPrivateProfileSectionsNames(byte[] lpszReturnBuffer, int nSize, string lpFileName);

  [DllImport("KERNEL32.DLL", EntryPoint="GetPrivateProfileStringA", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi, ExactSpelling=true)]
  private static extern int GetPrivateProfileString(string lpApplicationName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName);

  [DllImport("KERNEL32.DLL", EntryPoint="GetPrivateProfileStructA", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi, ExactSpelling=true)]
  private static extern int GetPrivateProfileStruct(string lpszSections, string lpszKey, byte[] lpStruct, int uSizeStruct, string szFile);
 
  [DllImport("KERNEL32.DLL", EntryPoint="WritePrivateProfileSectionsA", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi, ExactSpelling=true)]
  private static extern int WritePrivateProfileSections(string lpAppName, string lpString, string lpFileName);

  [DllImport("KERNEL32.DLL", EntryPoint="WritePrivateProfileStringA", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi, ExactSpelling=true)]
  private static extern int WritePrivateProfileString(string lpApplicationName, string lpKeyName, string lpString, string lpFileName);

  [DllImport("KERNEL32.DLL", EntryPoint="WritePrivateProfileStructA", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi, ExactSpelling=true)]
  private static extern int WritePrivateProfileStruct(string lpszSections, string lpszKey, byte[] lpStruct, int uSizeStruct, string szFile);

  #endregion


  private string _Filename;    //INI文件名
  private string _Sections;     //INI文件中配置参数的组别片段
  private const int MAX_ENTRY = 32768; //最大字符数


  public INIManage(string strFile)
  {
   this.Filename = strFile;
  }

  #region" INI操作类的属性 "

  public string Filename
  {
   get
   {
    return this._Filename;
   }
   set
   {
    this._Filename = value;
   }
  }


  public string Sections
  {
   get
   {
    return this._Sections;
   }
   set
   {
    this._Sections = value;
   }<