C# ini文件辅助操作类
C# ini文件辅助操作类
INI文件就是扩展名为“ini”的文件。 在Windows体系中,INI文件的使用有许多,最重要的就是“System.ini”、“System32.ini”和“Win.ini”。 该文件首要寄存用户所做的挑选以及体系的各种参数。用户能够经过修改INI文件,来改动应用程序和体系的许多装备。
自从Windows 95的退出,在Windows体系中引入了注册表的概念,INI文件在Windows体系的位置就开始不断下滑,这是由于注册表的共同长处,使应用程序和体系都把许多参数和初始化信息放进了注册表中。但在某些场合,INI文件还具有其不可代替的位置。下面是一个INI文件C#操作辅助类
/// <summary>
/// INI文件操作类
/// </summary>
public class INI
{
public string iniFile { get; private set; }
/// <summary>
///
/// </summary>
/// <param name="iniFile">文件地址</param>
public INI(string iniFile)
{
this.iniFile = iniFile;
}
#region WinAPI
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("Kernel32.dll")]
private extern static uint GetPrivateProfileStringA(string strAppName, string strKeyName, string sDefault, byte[] buffer, int nSize, string strFileName);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
#endregion
/// <summary>
/// 写入内容
/// </summary>
/// <param name="section">节点</param>
/// <param name="key">键名</param>
/// <param name="value">键值</param>
/// <returns></returns>
public long Write(string section, string key, string value)
{
if (!FileExists()) { File.Create(iniFile); }
long data = WritePrivateProfileString(section, key, value, this.iniFile);
return data;
}
/// <summary>
/// 获取内容
/// </summary>
/// <param name="section">节点</param>
/// <param name="key">键名</param>
/// <returns></returns>
public string Get(string section, string key)
{
if (!FileExists()) { return null; }
StringBuilder temp = new StringBuilder(1000);
int value = GetPrivateProfileString(section, key, "", temp, 500, this.iniFile);
return temp.ToString();
}
/// <summary>
/// 获取节点下所有键
/// </summary>
/// <param name="section">节点</param>
/// <returns></returns>
public ArrayList Keys(string section)
{
if (!FileExists()) { return null; }
byte[] buffer = new byte[5120];
uint rel = GetPrivateProfileStringA(section, null, "", buffer, buffer.GetUpperBound(0), this.iniFile);
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>
/// 获取所有Sections
/// </summary>
/// <returns></returns>
public List<string> Sections()
{
List<string> result = new List<string>();
Byte[] buf = new Byte[65536];
uint len = GetPrivateProfileStringA(null, null, null, buf, buf.Length, iniFile);
int j = 0;
for (int i = 0; i < len; i++)
if (buf[i] == 0)
{
result.Add(Encoding.Default.GetString(buf, j, i - j));
j = i + 1;
}
return result;
}
/// <summary>
/// 删除节点 逐级删除
/// </summary>
public void Remove(string section, string key = null)
{
if (!string.IsNullOrEmpty(section) && !string.IsNullOrEmpty(section))
{
WritePrivateProfileString(section, key, null, this.iniFile);
}
else if (!string.IsNullOrEmpty(section) && string.IsNullOrEmpty(section))
{
WritePrivateProfileString(section, null, null, this.iniFile);
}
}
private bool FileExists()
{
return File.Exists(this.iniFile);
}
}