Нет описания

IniFile.cs 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 CallCenterApi.Common
  8. {
  9. /// <summary>
  10. /// INI文件的操作类
  11. /// </summary>
  12. public class IniFile
  13. {
  14. public string Path;
  15. public IniFile(string path)
  16. {
  17. this.Path = path;
  18. }
  19. #region 声明读写INI文件的API函数
  20. [DllImport("kernel32")]
  21. private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
  22. [DllImport("kernel32")]
  23. private static extern int GetPrivateProfileString(string section, string key, string defVal, StringBuilder retVal, int size, string filePath);
  24. [DllImport("kernel32")]
  25. private static extern int GetPrivateProfileString(string section, string key, string defVal, Byte[] retVal, int size, string filePath);
  26. #endregion
  27. /// <summary>
  28. /// 写INI文件
  29. /// </summary>
  30. /// <param name="section">段落</param>
  31. /// <param name="key">键</param>
  32. /// <param name="iValue">值</param>
  33. public void IniWriteValue(string section, string key, string iValue)
  34. {
  35. WritePrivateProfileString(section, key, iValue, this.Path);
  36. }
  37. /// <summary>
  38. /// 读取INI文件
  39. /// </summary>
  40. /// <param name="section">段落</param>
  41. /// <param name="key">键</param>
  42. /// <returns>返回的键值</returns>
  43. public string IniReadValue(string section, string key)
  44. {
  45. StringBuilder temp = new StringBuilder(255);
  46. int i = GetPrivateProfileString(section, key, "", temp, 255, this.Path);
  47. return temp.ToString();
  48. }
  49. /// <summary>
  50. /// 读取INI文件
  51. /// </summary>
  52. /// <param name="Section">段,格式[]</param>
  53. /// <param name="Key">键</param>
  54. /// <returns>返回byte类型的section组或键值组</returns>
  55. public byte[] IniReadValues(string section, string key)
  56. {
  57. byte[] temp = new byte[255];
  58. int i = GetPrivateProfileString(section, key, "", temp, 255, this.Path);
  59. return temp;
  60. }
  61. }
  62. }