using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
namespace IniFile
{
public class IniFileOS
{
//文件inif名称
public string Path;
//------------声明都写INI的API 函数
//---写ini文件的api
[DllImport("kernel32")]
private static extern long WritePrivateProfileString
(string section, string key, string val, string filePath);
//section INI文件的段落 key 关键值 val 关键值的数值 filePath文件的完整路径和名称
//-----读ini 的api
[DllImport("kernel32")]
private static extern int GetPrivateProfileString
(string section, string key, string def, StringBuilder retVal, int size, string filePath);
//def 无法读取时的缺省值 retVal读取数值 size数值大小
//----------------本 类的构造函数 传递文件路径---
public IniFileOS(string inipath)
{
Path = inipath;
}
//-----写入ini文件 的 方法------------依次 段落 关键值 数值
public void iniWriteValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value,this.Path);
}
//-----读取ini文件 的方法
public string iniReadValue(string Section, string Key)
{
StringBuilder temp = new StringBuilder(500);
int i = GetPrivateProfileString(Section, Key, "", temp, 500, this.Path);
return temp.ToString();
}
// 删除段落
public void delSection(string Section)
{
WritePrivateProfileString(Section, null, null, this.Path);
}
// 删除key值
public void delKey(string Section, string Key)
{
WritePrivateProfileString(Section, Key, null, this.Path);
}
//--验证文件是否存在
public bool ExitIniFile()
{
return File.Exists(this.Path);
}
}
}