using System;
using System.IO;
using System.Text;
using System.Configuration;
using System.Runtime.InteropServices;
using System.Collections.Specialized;
using System.Collections;
using System.Collections.Generic;
/**//// <summary>
/// Summary description for IniFile
/// </summary>
namespace NativeWifi {
/**//// <summary>
/// 用于处理INI文件的类
/// </summary>
public class INIFile
{
// 说明:INI文件的名称必须使用绝对路径,原文如下 by DTY 09.4.15
//If the lpFileName parameter does not contain a full path and file name for the file,
//WritePrivateProfileString searches the Windows directory for the file.
//If the file does not exist,this function creates the file in the Windows directory.
string _FileName;
#region 导入DLL函数
//[DllImport("kernel32.dll")]
// private extern static int GetPrivateProfileIntA(string segName, string keyName, int iDefault, string fileName);
[DllImport("kernel32.dll")]
public extern static int GetPrivateProfileString(string segName, string keyName, string sDefault, StringBuilder buffer, int nSize, string fileName);
[DllImport("kernel32.dll")]
//public extern static int GetPrivateProfileStringA(string segName, string keyName, string sDefault, string buffer, int nSize, string fileName);
public extern static int GetPrivateProfileStringA(string segName, string keyName, string sDefault, byte[] buffer, int iLen, string fileName);
[DllImport("kernel32.dll")]
public extern static int GetPrivateProfileSection(string segName, StringBuilder buffer, int nSize, string fileName);
[DllImport("kernel32.dll")]
public extern static int WritePrivateProfileSection(string segName, string sValue, string fileName);
[DllImport("kernel32.dll")]
public extern static int WritePrivateProfileString(string segName, string keyName, string sValue, string fileName);
[DllImport("kernel32.dll")]
public extern static int GetPrivateProfileSectionNamesA(byte[] buffer, int iLen, string fileName);
#endregion
public INIFile(string FileName)
{
_FileName = FileName;
if (!FileExists())
CreateFile();
}
#region AboutFile
/**/
/// <summary>
/// 删除文件
/// </summary>
public void DeleteFile() {
if (FileExists())
File.Delete(_FileName);
}
/**/
/// <summary>
/// 创建文件
/// </summary>
public void CreateFile() {
File.Create(_FileName).Close();
}
/**/
/// <summary>
/// 判断文件是否存在
/// </summary>
/// <returns></returns>
public bool FileExists() {
return File.Exists(_FileName);
}
#endregion
#region Read
/**//// <summary>
/// 返回字符串
/// </summary>
public string ReadString(string Section, string Key) // 用StringBuilder只能读出第一行,不是一个好的写法 by DTY 09.4.15
{
StringBuilder buffer= new StringBuilder(65535);
GetPrivateProfileString(Section, Key, "", buffer, buffer.Capacity, _FileName);
return buffer.ToString();
}
/**//// <summary>
/// 返回int型的数
/// </summary>
public virtual int ReadInt(string Section, string Key)
{
int result;
try
{
result = int.Parse(this.ReadString(Section, Key));
}
catch
{
result = -1;
}
return result;
}
/**//// <summary>
/// 返回long型的数
/// </summary>
public virtual long ReadLong(string Section, string Key)
{
long result;
try
{
result = long.Parse(this.ReadString(Section, Key));
}
catch
{
result = -1;
}
return result;
}
/**//// <summary>
/// 返回byte型的数
/// </summary>
public virtual byte ReadByte(string Section, string Key)
{
byte result;
try
{
result = byte.Parse(this.ReadString(Section, Key));
}
catch
{
result = 0;
}
return result;
}
/**//// <summary>
/// 返回float型的数
/// </summary>
public virtual float ReadFloat(string Section, string Key)
{
float result;
try
{
result = float.Parse(this.ReadString(Section, Key));
}
catch
{
result = -1;
}
return result;
}
/**//// <summary>
/// 返回double型的数
/// </summary>
public virtual double ReadDouble(string Section, string Key)
{
double result;
try
{
result = double.Parse(this.ReadString(Section, Key));
}
catch
{
result = -1;
}
return result;
}
/**//// <summary>
/// 返回日期型的数
/// </summary>
public virtual DateTime ReadDateTime(string Section, string Key)
{
DateTime result;
try
{
result = DateTime.Parse(this.ReadString(Section, Key));
}
catch
{
result = DateTime.Parse("0-0-0"); ;
}
return result;
}
/**//// <summary>
/// 读bool量
/// </summary>
public virtual bool ReadBool(string Section, string Key)
{
bool result;
try
{
result = bool.Parse(this.ReadString(Section, Key));
}
catch
{
result = bool.Parse("0-0-0"); ;
}
return result;
}
#endregion _Endregion;
#region Write
/**//// <summary>
/// 用于写任何类型的键值到ini文件中
/// </summary>
/// <param name="Section">该键所在的节名称</param>
/// <param name="Key">该键的名称</param>
/// <param name="Value">该键的值</param>
public void Write(string Section, string Key, object Value)
{
if (Value!=null)
WritePrivateProfileString(Section, Key, Value.ToString(), _FileName);
else
WritePrivateProfileString(Section, Key, null, _FileName);
}
#endregion
#region others
/**//// <summary>
/// 返回该配置文件中所有Section名称的集合
/// </summary>
public ArrayList ReadSections()
{
byte[] buffer = new byte[65535];
int rel = GetPrivateProfileSectionNamesA(buffer, buffer.GetUpperBound(0), _FileName);
int iCnt, iPos;
ArrayList arrayList = new ArrayList();
string tmp;
if (rel>0)
{
iCnt = 0; iPos = 0;
for (iCnt = 0; iCnt < rel; iCnt++)
{
if (buffer[iCnt] == 0x00)
{
tmp =System.Text.ASCIIEncoding.Default.GetString(buffer, iPos, iCnt-iPos ).Trim();
iPos = iCnt + 1;
if (tmp != "")
arrayList.Add(tmp);
}
}
}
return arrayList;
}
// 获取节点的所有KEY值 by DTY 09.4.15
public ArrayList ReadKeys(string sectionName) {
byte[] buffer = new byte[5120];
int rel = GetPrivateProfileStringA(sectionName, null, "", buffer, buffer.GetUpperBound(0), _FileName);
int iCnt, iPos;
ArrayList arrayList = new ArrayList();
string tmp;
if (rel > 0) {
iCnt = 0; iPos = 0;
for (iCnt = 0; iCnt < rel; iCnt++) {
if (buffer[iCnt] == 0x00) {
tmp = System.Text.ASCIIEncoding.Default.GetString(buffer, iPos, iCnt-iPos).Trim();
iPos = iCnt + 1;
if (tmp != "")
arrayList.Add(tmp);
}
}
}
return arrayList;
}
/**//// <summary>
/// 判断指定的节是否存在
/// </summary>
public bool SectionExists(string Section)
{
//done SectionExists
StringBuilder buffer= new StringBuilder(65535);
GetPrivateProfileSection(Section, buffer, buffer.Capacity, _FileName);
if (buffer.ToString().Trim() == "")
return false;
else
return true;
}
/**//// <summary>
/// 判断指定的节中指定的键是否存在
/// </summary>
public bool ValueExits(string Section, string Key)
{
if (ReadString(Section, Key).Trim() == "")
return false;
else
return true;
}
/**//// <summary>
/// 删除指定的节中的指定键
/// </summary>
/// <param name="Section">该键所在的节的名称</param>
/// <param name="Key">该键的名称</param>
public void DeleteKey(string Section, string Key)
{
Write(Section, Key, null);
}
/**//// <summary>
/// 删除指定的节的所有内容
/// </summary>
/// <param name="Section">要删除的节的名字</param>
public void DeleteSection(string Section)
{
WritePrivateProfileSection(Section, null, _FileName);
}
/**//// <summary>
/// 添加一个节
/// </summary>
/// <param name="Section">要添加的节名称</param>
public void AddSection(string Section)
{
WritePrivateProfileSection(Section, "", _FileName);
}
public void AddSection(string Section, string key) {
WritePrivateProfileSection(Section, key, _FileName);
}
#endregion
}
}
本文转自左正博客园博客,原文链接:http://www.cnblogs.com/soundcode/archive/2010/12/19/1910612.html,如需转载请自行联系原作者