using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace JinYuan.Helper
{
public class IniConfigHelper
{
///
/// 文件路径
///
public static string iniFilePath = string.Empty;
#region API函数声明
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key,
string val, string filePath);
//需要调用GetPrivateProfileString的重载
[DllImport("kernel32", EntryPoint = "GetPrivateProfileString")]
private static extern long GetPrivateProfileString(string section, string key,
string def, StringBuilder retVal, int size, string filePath);
[DllImport("kernel32", EntryPoint = "GetPrivateProfileString")]
private static extern uint GetPrivateProfileStringA(string section, string key,
string def, Byte[] retVal, int size, string filePath);
#endregion
///
///
///
///
///
public static void CreateIniFile(string StartupPath, string AppName)
{
iniFilePath = StartupPath + AppName;
if (!Directory.Exists(StartupPath))
{
Directory.CreateDirectory(StartupPath);
}
if (!File.Exists(iniFilePath))
{
File.Create(iniFilePath).Close();
}
}
#region 读取INI文件
///
/// 根据节点及Key的值返回数据
///
/// 节点
/// 键
/// 默认值
/// 路径
/// 返回值
public static string ReadIniData(string Section, string Key, string NoText, string iniFilePath)
{
if (File.Exists(iniFilePath))
{
StringBuilder stringBuilder = new StringBuilder(10240);
GetPrivateProfileString(Section, Key, NoText, stringBuilder, 10240, iniFilePath);
return stringBuilder.ToString();
}
else
{
return string.Empty;
}
}
///
/// 根据节点及Key的值返回数据
///
/// 节点
/// 键
/// 默认值
/// 返回值
public static string ReadIniData(string Section, string Key, string NoText)
{
return ReadIniData(Section, Key, NoText, iniFilePath);
}
#endregion
#region 写入Ini文件
///
/// 根据节点及Key的值写入数据
///
/// 节点
/// 键
/// 值
/// 路径
/// 操作结果
public static bool WriteIniData(string Section, string Key, string Value, string path)
{
long result = WritePrivateProfileString(Section, Key, Value, path);
if (result == 0)
{
return false;
}
else
{
return true;
}
}
///
/// 根据节点及Key的值写入数据
///
/// 节点
/// 键
/// 值
/// 操作结果
public static bool WriteIniData(string Section, string Key, string Value)
{
return WriteIniData(Section, Key, Value, iniFilePath);
}
#endregion
#region 读取所有的Sections
///
/// 读取所有的Section
///
/// 路径
/// Section集合
public static List ReadSections(string path)
{
byte[] buffer = new byte[65536];
uint length = GetPrivateProfileStringA(null, null, null, buffer, buffer.Length, path);
int startIndex = 0;
List sections = new List();
for (int i = 0; i < length; i++)
{
if (buffer[i] == 0)
{
sections.Add(Encoding.Default.GetString(buffer, startIndex, i - startIndex));
startIndex = i + 1;
}
}
return sections;
}
///
/// 读取所有的Section
///
/// 路径
/// Section集合
public static List ReadSections()
{
byte[] buffer = new byte[65536];
uint length = GetPrivateProfileStringA(null, null, null, buffer, buffer.Length, iniFilePath);
int startIndex = 0;
List sections = new List();
for (int i = 0; i < length; i++)
{
if (buffer[i] == 0)
{
sections.Add(Encoding.Default.GetString(buffer, startIndex, i - startIndex));
startIndex = i + 1;
}
}
return sections;
}
#endregion
#region 根据某个Section读取所有的Keys
///
/// 根据某个Section读取所有的Keys
///
/// 某个section
/// 路径
/// key的集合
public static List ReadKeys(string section, string path)
{
byte[] buffer = new byte[65536];
uint length = GetPrivateProfileStringA(section, null, null, buffer, buffer.Length, path);
int startIndex = 0;
List keys = new List();
for (int i = 0; i < length; i++)
{
if (buffer[i] == 0)
{
keys.Add(Encoding.Default.GetString(buffer, startIndex, i - startIndex));
startIndex = i + 1;
}
}
return keys;
}
///
/// 根据某个Section读取所有的Keys
///
/// 某个section
/// 路径
/// key的集合
public static List ReadKeys(string section)
{
byte[] buffer = new byte[65536];
uint length = GetPrivateProfileStringA(section, null, null, buffer, buffer.Length, iniFilePath);
int startIndex = 0;
List keys = new List();
for (int i = 0; i < length; i++)
{
if (buffer[i] == 0)
{
keys.Add(Encoding.Default.GetString(buffer, startIndex, i - startIndex));
startIndex = i + 1;
}
}
return keys;
}
#endregion
}
}