INIFile.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Text;
  4. using System.IO;
  5. namespace Ant.Service.Utilities
  6. {
  7. /// <summary>
  8. /// INI文件读写类。
  9. /// </summary>
  10. public class INIClass
  11. {
  12. public string inipath;
  13. [DllImport("kernel32")]
  14. private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
  15. [DllImport("kernel32")]
  16. private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
  17. /// <summary>
  18. /// 构造方法
  19. /// </summary>
  20. /// <param name="INIPath">文件路径</param>
  21. public INIClass(string INIPath)
  22. {
  23. inipath = INIPath;
  24. }
  25. /// <summary>
  26. /// 写入INI文件
  27. /// </summary>
  28. /// <param name="Section">项目名称(如 [TypeName] )</param>
  29. /// <param name="Key">键</param>
  30. /// <param name="Value">值</param>
  31. public void IniWriteValue(string Section, string Key, string Value)
  32. {
  33. WritePrivateProfileString(Section, Key, Value, this.inipath);
  34. }
  35. /// <summary>
  36. /// 读出INI文件
  37. /// </summary>
  38. /// <param name="Section">项目名称(如 [TypeName] )</param>
  39. /// <param name="Key">键</param>
  40. public string IniReadValue(string Section, string Key)
  41. {
  42. StringBuilder temp = new StringBuilder(500);
  43. int i = GetPrivateProfileString(Section, Key, "", temp, 500, this.inipath);
  44. return temp.ToString();
  45. }
  46. /// <summary>
  47. /// 验证文件是否存在
  48. /// </summary>
  49. /// <returns>布尔值</returns>
  50. public bool ExistINIFile()
  51. {
  52. return File.Exists(inipath);
  53. }
  54. /// <summary>
  55. ///
  56. ///创建文件夹
  57. /// </summary>
  58. /// <param name="path">
  59. ///路径
  60. ///</param>
  61. private void NewDirectory(String path)
  62. {
  63. if (!Directory.Exists(path))
  64. {
  65. Directory.CreateDirectory(path);
  66. }
  67. }
  68. /// <summary>
  69. /// 添加一行注释
  70. /// </summary>
  71. /// <param name="Notes">注释</param>
  72. public void AddNotes(string Notes)
  73. {
  74. string filename = inipath;
  75. string path;
  76. path = Directory
  77. .GetParent(filename).ToString();
  78. NewDirectory(path);
  79. FileStream fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write);
  80. StreamWriter sw = new StreamWriter(fs);
  81. sw.BaseStream.Seek(0, SeekOrigin.End);
  82. sw.WriteLine(@";" + Notes);
  83. sw.Flush();
  84. sw.Close();
  85. fs.Close();
  86. sw.Dispose();
  87. fs.Dispose();
  88. }
  89. /// <summary>
  90. /// 添加一行文本
  91. /// </summary>
  92. /// <param name="Text">文本</param>
  93. public void AddText(string Text)
  94. {
  95. string filename = inipath;
  96. string path;
  97. path = Directory
  98. .GetParent(filename).ToString();
  99. NewDirectory(path);
  100. FileStream fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write);
  101. StreamWriter sw = new StreamWriter(fs);
  102. sw.BaseStream.Seek(0, SeekOrigin.End);
  103. sw.WriteLine(Text);
  104. sw.Flush();
  105. sw.Close();
  106. fs.Close();
  107. sw.Dispose();
  108. fs.Dispose();
  109. }
  110. }
  111. }