using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace YG.Lib
{
public static class TextApiLib
{
#region API函数声明
[DllImport("kernel32")]
public static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
//需要调用GetPrivateProfileString的重载
[DllImport("kernel32", EntryPoint = "GetPrivateProfileString")]
public static extern long GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
[DllImport("kernel32", EntryPoint = "GetPrivateProfileString")]
public static extern uint GetPrivateProfileStringA(string section, string key, string def, Byte[] retVal, int size, string filePath);
///
///在路径filepath中 在section下边查找key,如果没找到那么返回notfindvalue默认值,
///
///
///
/// 没有找到的返回之
///
///
[DllImport("kernel32", EntryPoint = "GetPrivateProfileInt")]
public static extern uint GetPrivateProfileInt(string section, string key, int notfindvalue, string filePath);
///
/// 查找ini文件中所有的section
///
///
///
///
///
[DllImport("kernel32", EntryPoint = "GetPrivateProfileSectionNames")]
private static extern int GetPrivateProfileSectionNames(byte[] sb, int notfindvalue, string filePath);
///
/// 获取section下的所有的key
///
///
///
///
///
///
[DllImport("KERNEL32.DLL", EntryPoint = "GetPrivateProfileSection")]
private static extern int GetPrivateProfileSection(string lpAppName, byte[] lpReturnedString, int nSize, string lpFileName);
#endregion
///
/// 获取文件下边所有的section
///
///
///
public static List GetPrivateProfileSectionNames(string filepath)
{
byte[] bt = new byte[1024 * 5];
int sectionleng = GetPrivateProfileSectionNames(bt, 1024, filepath);
string sectionnames = Encoding.ASCII.GetString(bt, 0, sectionleng);
string[] vs = sectionnames.Split(new string[] { "\0" }, StringSplitOptions.None);
return vs.Where(m => !string.IsNullOrEmpty(m)).ToList();
}
///
/// 获取selection下的所有的key
///
/// [FT01]类似表头
/// 脚本路径
///
public static List GetPrivateProfileSection(string selectname, string lpFileName)
{
byte[] bt = new byte[1024 * 5];
int keyleng = GetPrivateProfileSection(selectname, bt, 1024, lpFileName);
string keysvalue = Encoding.ASCII.GetString(bt, 0, keyleng);
//keysvalue=keysvalue.Replace("\0", "\",\"").Replace("=", "\":\"");
string[] vs = keysvalue.Split(new string[] { "\0" }, StringSplitOptions.None);
return vs.Where(m=>!string.IsNullOrEmpty(m)&&!m.StartsWith("//")).ToList();
}
}
}