C#操作操作INI文件的辅助类文件的辅助类IniHelper
使用INI配置文件,简单便捷。
该辅助工具类为C#操作INI文件的辅助类,源码在某位师傅的基础上完善的来,因为忘记最初的来源了,因此不能提及引用,
在此深感遗憾,并对贡献者表示感谢。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
namespace Eyuan.Common
{
public static class INIHelper
{
#region 读写INI文件相关
[DllImport("kernel32.dll", EntryPoint = "WritePrivateProfileString", CharSet = CharSet.Ansi)] private static extern long
WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32.dll", EntryPoint = "GetPrivateProfileString", CharSet = CharSet.Ansi)] private static extern int
GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
[DllImport("kernel32")] private static extern int GetPrivateProfileInt(string lpApplicationName, string lpKeyName, int nDefault,
string lpFileName);
[DllImport("kernel32.dll", EntryPoint = "GetPrivateProfileSectionNames", CharSet = CharSet.Ansi)] private static extern int
GetPrivateProfileSectionNames(IntPtr lpszReturnBuffer, int nSize, string filePath);
[DllImport("KERNEL32.DLL ", EntryPoint = "GetPrivateProfileSection", CharSet = CharSet.Ansi)] private static extern int
GetPrivateProfileSection(string lpAppName, byte[] lpReturnedString, int nSize, string filePath);
#endregion
#region 读写操作(字符串)
/// <summary>
/// 向INI写入数据
/// </summary>
/// <PARAM name="Section">节点名</PARAM>
/// <PARAM name="Key">键名</PARAM>
/// <PARAM name="Value">值(字符串)</PARAM>
public static void Write(string Section, string Key, string Value, string path)
{
WritePrivateProfileString(Section, Key, Value, path);
}
/// <summary>
/// 读取INI数据
/// </summary>
/// <PARAM name="Section">节点名</PARAM>
/// <PARAM name="Key">键名</PARAM>
/// <PARAM name="Path">值名</PARAM>
/// <returns>值(字符串)</returns>
public static string Read(string Section, string Key, string path)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "", temp, 255, path);
return temp.ToString();
}
#endregion
#region 配置节信息
/// <summary>
/// 读取一个ini里面所有的节
/// </summary>
/// <param name="sections"></param>
/// <param name="path"></param>
/// <returns>-1:没有节信息,0:正常</returns>
public static int GetAllSectionNames(out string[] sections, string path)