A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 马谦 中级黑马   /  2013-10-15 13:57  /  1617 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Text;
  4. using System.IO;
  5. namespace SuperCode.Common
  6. {
  7.   /// <summary>
  8.     /// INI文件读写类。
  9.     /// </summary>
  10. public class INIFile
  11. {
  12. public string path;
  13. public INIFile(string INIPath)
  14. {
  15. path = INIPath;
  16. }

  17. [DllImport("kernel32")]
  18. private static extern long WritePrivateProfileString(string section,string key,string val,string filePath);

  19. [DllImport("kernel32")]
  20. private static extern int GetPrivateProfileString(string section,string key,string def, StringBuilder retVal,int size,string filePath);

  21. [DllImport("kernel32")]
  22. private static extern int GetPrivateProfileString(string section, string key, string defVal, Byte[] retVal, int size, string filePath);

  23. /// <summary>
  24. /// 写INI文件
  25. /// </summary>
  26. /// <param name="Section"></param>
  27. /// <param name="Key"></param>
  28. /// <param name="Value"></param>
  29. public void IniWriteValue(string Section,string Key,string Value)
  30. {
  31. WritePrivateProfileString(Section,Key,Value,this.path);
  32. }

  33. /// <summary>
  34. /// 读取INI文件
  35. /// </summary>
  36. /// <param name="Section"></param>
  37. /// <param name="Key"></param>
  38. /// <returns></returns>
  39. public string IniReadValue(string Section,string Key)
  40. {
  41. StringBuilder temp = new StringBuilder(255);
  42. int i = GetPrivateProfileString(Section,Key,"",temp, 255, this.path);
  43. return temp.ToString();
  44. }
  45. public byte[] IniReadValues(string section, string key)
  46. {
  47. byte[] temp = new byte[255];
  48. int i = GetPrivateProfileString(section, key, "", temp, 255, this.path);
  49. return temp;

  50. }

  51. /// <summary>
  52. /// 删除ini文件下所有段落
  53. /// </summary>
  54. public void ClearAllSection()
  55. {
  56. IniWriteValue(null,null,null);
  57. }
  58. /// <summary>
  59. /// 删除ini文件下personal段落下的所有键
  60. /// </summary>
  61. /// <param name="Section"></param>
  62. public void ClearSection(string Section)
  63. {
  64. IniWriteValue(Section,null,null);
  65. }
  66. }
  67. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
追溯客 + 1

查看全部评分

2 个回复

倒序浏览
值得学习ing!
回复 使用道具 举报
学习ing     代码整理一下   
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马