LibTextApi.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace YG.Lib
  8. {
  9. public static class TextApiLib
  10. {
  11. #region API函数声明
  12. [DllImport("kernel32")]
  13. public static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
  14. //需要调用GetPrivateProfileString的重载
  15. [DllImport("kernel32", EntryPoint = "GetPrivateProfileString")]
  16. public static extern long GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
  17. [DllImport("kernel32", EntryPoint = "GetPrivateProfileString")]
  18. public static extern uint GetPrivateProfileStringA(string section, string key, string def, Byte[] retVal, int size, string filePath);
  19. /// <summary>
  20. ///在路径filepath中 在section下边查找key,如果没找到那么返回notfindvalue默认值,
  21. /// </summary>
  22. /// <param name="section"></param>
  23. /// <param name="key"></param>
  24. /// <param name="notfindvalue">没有找到的返回之</param>
  25. /// <param name="filePath"></param>
  26. /// <returns></returns>
  27. [DllImport("kernel32", EntryPoint = "GetPrivateProfileInt")]
  28. public static extern uint GetPrivateProfileInt(string section, string key, int notfindvalue, string filePath);
  29. /// <summary>
  30. /// 查找ini文件中所有的section
  31. /// </summary>
  32. /// <param name="section"></param>
  33. /// <param name="notfindvalue"></param>
  34. /// <param name="filePath"></param>
  35. /// <returns></returns>
  36. [DllImport("kernel32", EntryPoint = "GetPrivateProfileSectionNames")]
  37. private static extern int GetPrivateProfileSectionNames(byte[] sb, int notfindvalue, string filePath);
  38. /// <summary>
  39. /// 获取section下的所有的key
  40. /// </summary>
  41. /// <param name="lpAppName"></param>
  42. /// <param name="lpReturnedString"></param>
  43. /// <param name="nSize"></param>
  44. /// <param name="lpFileName"></param>
  45. /// <returns></returns>
  46. [DllImport("KERNEL32.DLL", EntryPoint = "GetPrivateProfileSection")]
  47. private static extern int GetPrivateProfileSection(string lpAppName, byte[] lpReturnedString, int nSize, string lpFileName);
  48. #endregion
  49. /// <summary>
  50. /// 获取文件下边所有的section
  51. /// </summary>
  52. /// <param name="filepath"></param>
  53. /// <returns></returns>
  54. public static List<string> GetPrivateProfileSectionNames(string filepath)
  55. {
  56. byte[] bt = new byte[1024 * 5];
  57. int sectionleng = GetPrivateProfileSectionNames(bt, 1024, filepath);
  58. string sectionnames = Encoding.ASCII.GetString(bt, 0, sectionleng);
  59. string[] vs = sectionnames.Split(new string[] { "\0" }, StringSplitOptions.None);
  60. return vs.Where(m => !string.IsNullOrEmpty(m)).ToList();
  61. }
  62. /// <summary>
  63. /// 获取selection下的所有的key
  64. /// </summary>
  65. /// <param name="selectname">[FT01]类似表头</param>
  66. /// <param name="lpFileName">脚本路径</param>
  67. /// <returns></returns>
  68. public static List<string> GetPrivateProfileSection(string selectname, string lpFileName)
  69. {
  70. byte[] bt = new byte[1024 * 5];
  71. int keyleng = GetPrivateProfileSection(selectname, bt, 1024, lpFileName);
  72. string keysvalue = Encoding.ASCII.GetString(bt, 0, keyleng);
  73. //keysvalue=keysvalue.Replace("\0", "\",\"").Replace("=", "\":\"");
  74. string[] vs = keysvalue.Split(new string[] { "\0" }, StringSplitOptions.None);
  75. return vs.Where(m=>!string.IsNullOrEmpty(m)&&!m.StartsWith("//")).ToList();
  76. }
  77. }
  78. }