.NET下INI配置文件操作类

 
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;
   }
  }

  #endregion

  #region" INI操作类的Read相关方法 "
 
  // Read相关方法中的 DefaultValue是 在INI文件中找不到相关配置 时的返回值
  //ReadBoolean是读取bool类型的配置参数,ReadByteArray是读取 byte[]类型的配置参数
  //ReadInteger是读取int类型的配置参数。。。。依次类推

  public bool ReadBoolean(string Key)
  {
   return this.ReadBoolean(this.Sections, Key);
  }

  public bool ReadBoolean(string Key, bool DefaultValue)
  {
   return this.ReadBoolean(this.Sections, Key, DefaultValue);
  }

  public bool ReadBoolean(string Sections, string Key)
  {
   return this.ReadBoolean(Sections, Key, false);
  }

  public bool ReadBoolean(string Sections, string Key, bool DefaultValue)
  {
   return bool.Parse(this.ReadString(Sections, Key, DefaultValue.ToString()));
  }

  public byte[] ReadByteArray(string Key, int Length)
  {
   return this.ReadByteArray(this.Sections, Key, Length);
  }

  public byte[] ReadByteArray(string Sections, string Key, int Length)
  {
   byte[] buffer1;
   if (Length > 0)
   {
    try
    {
     byte[] buffer2 = new byte[(Length - 1) + 1];
     if (INIManage.GetPrivateProfileStruct(Sections, Key, buffer2, buffer2.Length, this.Filename) == 0)
     {
      return null;
     }
     return buffer2;
    }
    catch (Exception exception1)
    {
     ProjectData.SetProjectError(exception1);
     buffer1 = null;
     ProjectData.ClearProjectError();
     return buffer1;               
    }
   }
   else
   {
    return null;
   }
       
  }

  public int ReadInteger(string Key)
  {
   return this.ReadInteger(Key, 0);
  }

  public int ReadInteger(string Key, int DefaultValue)
  {
   return this.ReadInteger(this.Sections, Key, DefaultValue);
  }

  public int ReadInteger(string Sections, string Key)
  {
   return this.ReadInteger(Sections, Key, 0);
  }

  public int ReadInteger(string Sections, string Key, int DefaultValue)
  {
   int num1;
   try
   {
    num1 = INIManage.GetPrivateProfileInt(Sections, Key, DefaultValue, this.Filename);
   }
   catch (Exception exception1)
   {
    ProjectData.SetProjectError(exception1);
    num1 = DefaultValue;
    ProjectData.ClearProjectError();
    return num1;           
   }
   return num1;
  }

  public long ReadLong(string Key)
  {
   return this.ReadLong(Key, (long) 0);
  }

  public long ReadLong(string Key, long DefaultValue)
  {
   return this.ReadLong(this.Sections, Key, DefaultValue);
  }

  public long ReadLong(string Sections, string Key)
  {
   return this.ReadLong(Sections, Key, 0);
  }

  public long ReadLong(string Sections, string Key, long DefaultValue)
  {
   return long.Parse(this.ReadString(Sections, Key, DefaultValue.ToString()));
  }

  public string ReadString(string Key)
  {
   return this.ReadString(this.Sections, Key);
  }

  public string ReadString(string Sections, string Key)
  {
   return this.ReadString(Sections, Key, "");
  }

  public string ReadString(string Sections, string Key, string DefaultValue)
  {
   string text1;
   try
   {
    StringBuilder builder1 = new StringBuilder(MAX_ENTRY);
    int num1 = INIManage.GetPrivateProfileString(Sections, Key, DefaultValue, builder1, MAX_ENTRY, this.Filename);
    text1 = builder1.ToString();
   }
   catch (Exception exception1)
   {
    ProjectData.SetProjectError(exception1);
    text1 = DefaultValue;
    ProjectData.ClearProjectError();
    return text1;
         
   }
   return text1;
  }

  #endregion

  #region" INI操作类的Write相关方法 "
 
  public bool Write(string Key, bool Value)
  {
   return this.Write(this.Sections, Key, Value);
  }

  public bool Write(string Key, byte[] Value)
  {
   return this.Write(this.Sections, Key, Value);
  }

  public bool Write(string Key, string Value)
  {
   return this.Write(this.Sections, Key, Value);
  }

  public bool Write(string Key, int Value)
  {
   return this.Write(this.Sections, Key, Value);
  }

  public bool Write(string Key, long Value)
  {
   return this.Write(this.Sections, Key, Value);
  }

  public bool Write(string Sections, string Key, byte[] Value)
  {
   bool flag1;
   try
   {
    flag1 = INIManage.WritePrivateProfileStruct(Sections, Key, Value, Value.Length, this.Filename) != 0;
   }
   catch (Exception exception1)
   {
    ProjectData.SetProjectError(exception1);
    flag1 = false;
    ProjectData.ClearProjectError();
    return flag1;          
   }
   return flag1;
  }

  public bool Write(string Sections, string Key, bool Value)
  {
   return this.Write(Sections, Key, Value.ToString());
  }

  public bool Write(string Sections, string Key, int Value)
  {
   bool flag1;
   try
   {
    flag1 = INIManage.WritePrivateProfileString(Sections, Key, Value.ToString(), this.Filename) != 0;
   }
   catch (Exception exception1)
   {
    ProjectData.SetProjectError(exception1);
    flag1 = false;
    ProjectData.ClearProjectError();
    return flag1;          
   }
   return flag1;
  }

  public bool Write(string Sections, string Key, long Value)
  {
   return this.Write(Sections, Key, Value.ToString());
  }

  public bool Write(string Sections, string Key, string Value)
  {
   bool flag1;
   try
   {
    flag1 = INIManage.WritePrivateProfileString(Sections, Key, Value, this.Filename) != 0;
   }
   catch (Exception exception1)
   {
    ProjectData.SetProjectError(exception1);
    flag1 = false;
    ProjectData.ClearProjectError();
    return flag1;          
   }
   return flag1;
  }

  #endregion

  #region" INI操作类的Delete相关方法 "

  public bool DeleteKey(string Key)
  {
   bool flag1;
   try
   {
    flag1 = INIManage.WritePrivateProfileString(this.Sections, Key, null, this.Filename) != 0;
   }
   catch (Exception exception1)
   {
    ProjectData.SetProjectError(exception1);
    flag1 = false;
    ProjectData.ClearProjectError();
    return flag1;           
   }
   return flag1;
  }

  public bool DeleteKey(string Section, string Key)
  {
   bool flag1;
   try
   {
    flag1 = INIManage.WritePrivateProfileString(Sections, Key, null, this.Filename) != 0;
   }
   catch (Exception exception1)
   {
    ProjectData.SetProjectError(exception1);
    flag1 = false;
    ProjectData.ClearProjectError();
    return flag1;          
   }
   return flag1;
  }

  public bool DeleteSections(string Section)
  {
   bool flag1;
   try
   {
    flag1 = INIManage.WritePrivateProfileSections(Sections, null, this.Filename) != 0;
   }
   catch (Exception exception1)
   {
    ProjectData.SetProjectError(exception1);
    flag1 = false;
    ProjectData.ClearProjectError();
    return flag1;          
   }
   return flag1;
  }

  #endregion

  public ArrayList GetSectionsNames()
  {
   int num1;
   ArrayList list1 = new ArrayList();
   byte[] buffer1 = new byte[MAX_ENTRY];
   int num2 = 0;
   try
   {
    num1 = INIManage.GetPrivateProfileSectionsNames(buffer1, MAX_ENTRY, this.Filename);
   }
   catch (Exception exception1)
   {
    ProjectData.SetProjectError(exception1);
    ProjectData.ClearProjectError();
    return list1;         
   }
   ASCIIEncoding encoding1 = new ASCIIEncoding();
   if (num1 > 0)
   {
    string text1 = encoding1.GetString(buffer1);
    num1 = 0;
    num2 = -1;
    while (true)
    {
     num1 = text1.IndexOf('\0', (int) (num2 + 1));
     if (((num1 - num2) == 1) || (num1 == -1))
     {
      return list1;
     }
     try
     {
      list1.Add(text1.Substring(num2 + 1, num1 - num2));
     }
     catch (Exception exception2)
     {
      ProjectData.SetProjectError(exception2);
      ProjectData.ClearProjectError();
     }
     num2 = num1;
    }
   }
   return list1;
  }

 }
}

时间: 2024-08-22 14:18:39

.NET下INI配置文件操作类的相关文章

C++读写INI配置文件的类实例_C 语言

本文实例讲述了C++读写INI配置文件的类.分享给大家供大家参考.具体如下: 1. IniReader.h文件: #ifndef INIREADER_H #define INIREADER_H #include <windows.h> class CIniReader { public: CIniReader(LPCTSTR szFileName); int ReadInteger(LPCTSTR szSection, LPCTSTR szKey, int iDefaultValue); fl

Python实现的ini文件操作类分享_python

类代码: # -*- coding:gbk -*- import ConfigParser, os class INIFILE: def __init__(self, filename): self.filename = filename self.initflag = False self.cfg = None self.readhandle = None self.writehandle = None def Init(self): self.cfg = ConfigParser.Confi

asp.net下Cache 缓存操作类代码

复制代码 代码如下: using System.Collections.Generic; using System.Web; using System; namespace DataAccess { /// <summary> /// 缓存控制类 /// </summary> public class CacheControl { public static List<string> AllUseCacheKey = new List<string>();

php下mysql数据库操作类(改自discuz)_php技巧

复制代码 代码如下: <?php /* -------------------------------- System:PT book - PT小说小偷 Code: 杰少Pakey ----------------------------------- */ $pt_mysql = new dbQuery; /** * mysql查询类 * */ class dbQuery { /** * 查询总次数 * * @var int */ var $querynum = 0; /** * 连接句柄 *

asp.net下Cache 缓存操作类代码_实用技巧

复制代码 代码如下: using System.Collections.Generic; using System.Web; using System; namespace DataAccess { /// <summary> /// 缓存控制类 /// </summary> public class CacheControl { public static List<string> AllUseCacheKey = new List<string>();

用java读写ini配置文件

??? 在java中,配置文件一般主要是两种形式:xml文件或者property文件.但大部分人都习惯使用ini文件,而且ini文件的分节以及注释功能,比起xml,也是易懂易用的. ??? 在vc中类库中有读写ini文件的标准函数.在dephi或其他语言中,也可以用windows的api函数来读写ini文件.但在java中似乎没有现成的类和方法可供使用.虽然java可以通过加载dll文件的方法来调用windows的api,但总觉得不够正宗. ??? 于是自己写了个读写ini配置文件的类,供大家参

C++读取INI配置文件类实例详解_C 语言

本文以实例讲解了C++读取配置文件的方法. 一般情况下,我们都喜欢使用ini扩展名的文件作为配置文件,可以读取及修改变量数值,也可以设置新的组,新的变量,本文的实例代码一个是读取INI的定义文件,另一个是CIniFile类实现文件,两者结合,完美实现VC++对INI文件的读写. 用户接口说明:在成员函数SetVarStr和SetVarInt函数中,当iType等于零,则如果用户制定的参数在ini文件中不存在,则就写入新的变量.当iType不等于零,则如果用户制定的参数在ini文件中不存在,就不写

一个.net下通用的Cookie操作类

笔者曾经参与一个大型多用户商城的开发,系统用户角色很多,有买家,卖家,代理商,系统管理员 ,普通管理员,超级管理员等,这些用户都涉及到登陆系统,然后建立cookies的问题,由于角色的不同 ,这些用户登陆系统后,所建的cookies都相对独立,而且对一个多用户商城来说,cookies区分域很重要 ,不同的域名要建不同的域.因此笔者在开发的过程中,写了很多次的建cookies,取cookies的代码,不 胜其烦! 既然都是建cookies,那么肯定有共同点,我们何不抽象出来,写一个通用的Cooki

Web项目: Java在部署项目的WebRoot下建立文件夹(附上文件操作类)

public boolean doTest(){ String path="../webapps/FileTest/reportFiles/aa.jsp";//FileTest为自己的项目名 reportFiles为自己建立的文件夹 aa.jsp为自己建立的文件 boolean isDone = false; File file = new File(path); if(file.exists()) throw new RuntimeException("File: &quo