IniFileHelper.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System;
  2. using System.IO;
  3. using System.Runtime.InteropServices;
  4. using System.Text;
  5. namespace Core.ONH.Collection.comm
  6. {
  7. class IniFileHelper
  8. {
  9. #region API函数声明
  10. [DllImport("kernel32")]//返回0表示失败,非0为成功
  11. private static extern long WritePrivateProfileString(string section, string key,
  12. string val, string filePath);
  13. [DllImport("kernel32")]//返回取得字符串缓冲区的长度
  14. private static extern long GetPrivateProfileString(string section, string key,
  15. string def, StringBuilder retVal, int size, string filePath);
  16. #endregion
  17. #region 读Ini文件
  18. public static string ReadIniData(string Section, string Key, string NoText, string iniFilePath)
  19. {
  20. if (File.Exists(iniFilePath))
  21. {
  22. StringBuilder temp = new StringBuilder(1024);
  23. GetPrivateProfileString(Section, Key, NoText, temp, 1024, iniFilePath);
  24. return temp.ToString();
  25. }
  26. else
  27. {
  28. return String.Empty;
  29. }
  30. }
  31. #endregion
  32. #region 写Ini文件
  33. public static bool WriteIniData(string section, string Key, string Value, string iniFilePath)
  34. {
  35. if (File.Exists(iniFilePath))
  36. {
  37. long opStation = WritePrivateProfileString(section, Key, Value, iniFilePath);
  38. if (opStation == 0)
  39. {
  40. return false;
  41. }
  42. return true;
  43. }
  44. return false;
  45. }
  46. #endregion
  47. }
  48. }