123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- 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);
- /// <summary>
- ///在路径filepath中 在section下边查找key,如果没找到那么返回notfindvalue默认值,
- /// </summary>
- /// <param name="section"></param>
- /// <param name="key"></param>
- /// <param name="notfindvalue">没有找到的返回之</param>
- /// <param name="filePath"></param>
- /// <returns></returns>
- [DllImport("kernel32", EntryPoint = "GetPrivateProfileInt")]
- public static extern uint GetPrivateProfileInt(string section, string key, int notfindvalue, string filePath);
- /// <summary>
- /// 查找ini文件中所有的section
- /// </summary>
- /// <param name="section"></param>
- /// <param name="notfindvalue"></param>
- /// <param name="filePath"></param>
- /// <returns></returns>
- [DllImport("kernel32", EntryPoint = "GetPrivateProfileSectionNames")]
- private static extern int GetPrivateProfileSectionNames(byte[] sb, int notfindvalue, string filePath);
- /// <summary>
- /// 获取section下的所有的key
- /// </summary>
- /// <param name="lpAppName"></param>
- /// <param name="lpReturnedString"></param>
- /// <param name="nSize"></param>
- /// <param name="lpFileName"></param>
- /// <returns></returns>
- [DllImport("KERNEL32.DLL", EntryPoint = "GetPrivateProfileSection")]
- private static extern int GetPrivateProfileSection(string lpAppName, byte[] lpReturnedString, int nSize, string lpFileName);
- #endregion
- /// <summary>
- /// 获取文件下边所有的section
- /// </summary>
- /// <param name="filepath"></param>
- /// <returns></returns>
- public static List<string> 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();
- }
- /// <summary>
- /// 获取selection下的所有的key
- /// </summary>
- /// <param name="selectname">[FT01]类似表头</param>
- /// <param name="lpFileName">脚本路径</param>
- /// <returns></returns>
- public static List<string> 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();
- }
- }
- }
|