IniFiles.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using System.Runtime.InteropServices;
  6. namespace LYFZ.WinAPI
  7. {
  8. /// <summary>
  9. /// Ini文件操作类
  10. /// </summary>
  11. public class IniFiles
  12. {
  13. public delegate void EventHandler(object sender, EventArgs e);
  14. public event EventHandler IniFileChanged;
  15. public event EventHandler Initialization;
  16. protected string IniFileName;
  17. /// <summary>
  18. /// 文件名
  19. /// </summary>
  20. public string FileName
  21. {
  22. get
  23. {
  24. return IniFileName;
  25. }
  26. set
  27. {
  28. if (value != IniFileName)
  29. {
  30. IniFileName = value;
  31. OnIniFileChanged(new EventArgs());
  32. }
  33. }
  34. }
  35. protected void OnIniFileChanged(EventArgs e)
  36. {
  37. if (IniFileChanged != null)
  38. IniFileChanged(null, e);
  39. }
  40. protected void OnInitialization(EventArgs e)
  41. {
  42. if (Initialization != null)
  43. Initialization(null, e);
  44. }
  45. [DllImport("kernel32")]
  46. private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
  47. /*
  48. section: 要写入的段落名
  49. key: 要写入的键,如果该key存在则覆盖写入
  50. val: key所对应的值
  51. filePath: INI文件的完整路径和文件名
  52. */
  53. [DllImport("kernel32")]
  54. private static extern int GetPrivateProfileString(string section, string key, string defVal, System.Text.StringBuilder retVal, int size, string filePath);
  55. /*
  56. section:要读取的段落名
  57. key: 要读取的键
  58. defVal: 读取异常的情况下的缺省值
  59. retVal: key所对应的值,如果该key不存在则返回空值
  60. size: 值允许的大小
  61. filePath: INI文件的完整路径和文件名
  62. */
  63. /* [DllImport("kernel32")]
  64. public static extern bool WritePrivateProfileString(byte[] section, byte[] key, byte[] val, string filePath);
  65. [DllImport("kernel32")]
  66. public static extern int GetPrivateProfileString(byte[] section, byte[] key, byte[] def, byte[] retVal, int size, string filePath);
  67. //与ini交互必须统一编码格式
  68. private static byte[] getBytes(string s, string encodingName)
  69. { return null == s ? null : Encoding.GetEncoding(encodingName).GetBytes(s); }
  70. public static string ReadString(string section, string key, string def, string fileName, string encodingName = "utf-8", int size = 40960)
  71. {
  72. byte[] buffer = new byte[size];
  73. int count = GetPrivateProfileString(getBytes(section, encodingName), getBytes(key, encodingName), getBytes(def, encodingName), buffer, size, fileName);
  74. return Encoding.GetEncoding(encodingName).GetString(buffer, 0, count).Trim();
  75. }
  76. public static bool WriteString(string section, string key, string value, string fileName, string encodingName = "utf-8")
  77. { return WritePrivateProfileString(getBytes(section, encodingName), getBytes(key, encodingName), getBytes(value, encodingName), fileName); }
  78. */
  79. /// <summary>
  80. /// 构造方法
  81. /// </summary>
  82. /// <param name="INIPath">文件路径</param>
  83. public IniFiles(string FileName)
  84. {
  85. IniFileName = FileName;
  86. }
  87. /// <summary>
  88. /// 写入INI文件
  89. /// </summary>
  90. /// <param name="Section">项目名称(如 [TypeName] )</param>
  91. /// <param name="Key">键</param>
  92. /// <param name="Value">值</param>
  93. public void WriteValue(string Section, string Key, string Value)
  94. {
  95. WritePrivateProfileString(Section, Key, Value, this.IniFileName);
  96. // WriteString(Section, Key, Value, this.IniFileName);
  97. }
  98. /// <summary>
  99. /// 删除指定段、键的值
  100. /// </summary>
  101. /// <param name="Section">要删除的值所在的段</param>
  102. /// <param name="Key">要删除的值所在的键</param>
  103. public void DeleteValue(string Section, string Key)
  104. {
  105. WritePrivateProfileString(Section, Key, null, this.IniFileName);
  106. // WriteString(Section, Key, null, this.IniFileName);
  107. }
  108. /// <summary>
  109. /// 删除指定段的值
  110. /// </summary>
  111. /// <param name="Section">要删除的值所在的段</param>
  112. public void DeleteValue(string Section)
  113. {
  114. WritePrivateProfileString(Section, null, null, this.IniFileName);
  115. // WriteString(Section, null, null, this.IniFileName);
  116. }
  117. /// <summary>
  118. /// 读出INI文件
  119. /// </summary>
  120. /// <param name="Section">项目名称(如 [TypeName] )</param>
  121. /// <param name="Key">键</param>
  122. public string ReadValue(string Section, string Key, string Default)
  123. {
  124. StringBuilder temp = new StringBuilder(5000);
  125. int i = GetPrivateProfileString(Section, Key, Default, temp, 5000, this.IniFileName);
  126. return temp.ToString();
  127. //return ReadString(Section, Key, Default, this.IniFileName);
  128. }
  129. /// <summary>
  130. /// 验证文件是否存在
  131. /// </summary>
  132. /// <returns>布尔值</returns>
  133. public bool ExistINIFile()
  134. {
  135. return File.Exists(IniFileName);
  136. }
  137. /// <summary>
  138. /// 创建文件夹
  139. /// </summary>
  140. /// <param name="path">路径</param>
  141. private void NewDirectory(String path)
  142. {
  143. if (!Directory.Exists(path))
  144. {
  145. Directory.CreateDirectory(path);
  146. }
  147. }
  148. /// <summary>
  149. /// 添加一行注释
  150. /// </summary>
  151. /// <param name="Notes">注释</param>
  152. public void AddNotes(string Notes)
  153. {
  154. string filename = IniFileName;
  155. string path;
  156. path = Directory.GetParent(filename).ToString();
  157. NewDirectory(path);
  158. FileStream fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write);
  159. StreamWriter sw = new StreamWriter(fs);
  160. sw.BaseStream.Seek(0, SeekOrigin.End);
  161. sw.WriteLine(@";" + Notes);
  162. sw.Flush();
  163. sw.Close();
  164. fs.Close();
  165. sw.Dispose();
  166. fs.Dispose();
  167. }
  168. /// <summary>
  169. /// 添加一行文本
  170. /// </summary>
  171. /// <param name="Text">文本</param>
  172. public void AddText(string Text)
  173. {
  174. string filename = IniFileName;
  175. string path;
  176. path = Directory.GetParent(filename).ToString();
  177. NewDirectory(path);
  178. FileStream fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write);
  179. StreamWriter sw = new StreamWriter(fs);
  180. sw.BaseStream.Seek(0, SeekOrigin.End);
  181. sw.WriteLine(Text);
  182. sw.Flush();
  183. sw.Close();
  184. fs.Close();
  185. sw.Dispose();
  186. fs.Dispose();
  187. }
  188. #region 重载
  189. public void WriteValue(string Section, string Key, int Value)
  190. {
  191. WriteValue(Section, Key, Value.ToString());
  192. }
  193. public void WriteValue(string Section, string Key, Boolean Value)
  194. {
  195. WriteValue(Section, Key, Value.ToString());
  196. }
  197. public void WriteValue(string Section, string Key, DateTime Value)
  198. {
  199. WriteValue(Section, Key, Value.ToString());
  200. }
  201. public void WriteValue(string Section, string Key, object Value)
  202. {
  203. WriteValue(Section, Key, Value.ToString());
  204. }
  205. public int ReadValue(string Section, string Key, int Default)
  206. {
  207. return Convert.ToInt32(ReadValue(Section, Key, Default.ToString()));
  208. }
  209. public bool ReadValue(string Section, string Key, bool Default)
  210. {
  211. return Convert.ToBoolean(ReadValue(Section, Key, Default.ToString()));
  212. }
  213. public DateTime ReadValue(string Section, string Key, DateTime Default)
  214. {
  215. return Convert.ToDateTime(ReadValue(Section, Key, Default.ToString()));
  216. }
  217. public string ReadValue(string Section, string Key)
  218. {
  219. return ReadValue(Section, Key, "");
  220. }
  221. #endregion
  222. }
  223. }