Ini文件操作类
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace IniOp
{
public class IniOperator
{
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal,int size,string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string defVal,Byte[]retVal,int size, string filePath);
/// <summary>
/// 写INI文件
/// </summary>
/// <param name="section">短节</param>
/// <param name="key">键</param>
/// <param name="value">键值</param>
/// <param name="path">写入的路径</param>
public void IniWriteValue(string section, string key, string value,string path)
{
WritePrivateProfileString(section, key, value, path);
}
/// <summary>
/// 删除指定路径下的Ini文件下所有段落
/// </summary>
public void ClearAllSection(string path)
{
IniWriteValue(null,null,null,path);
}
/// <summary>
/// 删除Ini文件下personal段落下的所有键
/// </summary>
/// <param name="section">段节</param>
/// <param name="path">路径</param>
public void ClearSection(string section,string path)
{
IniWriteValue(section,null,null,path);
}
/// <summary>
/// 读取Ini文件,返回字符串
/// </summary>
/// <param name="section">段节</param>
/// <param name="key">键</param>
/// <param name="path">路径</param>
/// <returns>返回键的值</returns>
public string IniReadValue(string section, string key,string path)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(section,key,"",temp,255,path);
return temp.ToString();
}
/// <summary>
/// 读取Ini文件,返回byte型数据
/// </summary>
/// <param name="section">段节</param>
/// <param name="key">键</param>
/// <param name="path">路径</param>
/// <returns>返回键的值</returns>
public byte[] IniReadValues(string section, string key, string path)
{
byte[] temp = new byte[255];
int i = GetPrivateProfileString(section, key, "", temp, 255, path);
return temp;
}
/// <summary>
/// 读取Ini文件的所有段落名
/// </summary>
public string[] IniReadValues(string path)
{
byte[] allSection = IniReadValues(null,null,path);
return ByteToString(allSection);
}
/// <summary>
/// 转换byte[]型为string[]数组类型
/// </summary>
/// <param name="sectionByte"></param>
/// <returns></returns>
private string[] ByteToString(byte[] sectionByte)
{
ASCIIEncoding ascii = new ASCIIEncoding();
//编写所有key的string类型
string sections = ascii.GetString(sectionByte);
//获取key的数组
string[] sectionList = sections.Split(new char[1]{'\0'});
return sectionList;
}
/// <summary>
/// 读取ini文件的某个段落下的所有键名
/// </summary>
/// <param name="section"></param>
/// <param name="path"></param>
/// <returns></returns>
public string[] IniReaValues(string section, string path)
{
byte[] sectionByte = IniReadValues(section,null,path);
return ByteToString(sectionByte);
}
}
}