CBlackBoxFile.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.IO;
  7. namespace BlackBoxFile
  8. {
  9. /// <summary>
  10. /// 黑匣子
  11. /// </summary>
  12. public class CBlackBoxFile
  13. {
  14. private static readonly int FILE_LENGTH = 1024 * 100;//100K
  15. private static readonly int FILE_BACK_LENGTH = 1024 * 1024;//1M,可以根据实际情况调整
  16. private static readonly int MAX_NUMBER_FILE = 3;//备份文件的个数+1,为4
  17. //private static readonly string strDir = System.IO.Directory.GetCurrentDirectory() + "\\MyFile";
  18. //private static readonly string strBakDir = System.IO.Directory.GetCurrentDirectory() + "\\MyBakFile";
  19. private static readonly string strDir = "C:\\MyFile";
  20. private static readonly string strBakDir = "C:\\MyBakFile";
  21. private List<string> mFileList = new List<string>();
  22. private static Mutex m_MutexSection = new Mutex();
  23. internal CBlackBoxFile()
  24. {
  25. }
  26. /// <summary>
  27. /// 黑匣子记录的入口
  28. /// </summary>
  29. /// <param name="uFileName">文件的编号</param>
  30. /// <param name="fileContext">要记录的信息</param>
  31. public void WriteFile(ushort uFileName, string fileContext)
  32. {
  33. fileContext = DateTime.Now.ToString() + ": " + fileContext;
  34. DirectoryInfo dir = new DirectoryInfo(strDir);
  35. if (!dir.Exists)
  36. {
  37. CreateDir(strDir);
  38. }
  39. string strFileName = uFileName + ".txt";
  40. string fname = strDir + "\\" + strFileName;
  41. FileInfo finfo = new FileInfo(fname);
  42. if (finfo.Exists && finfo.Length > FILE_LENGTH)
  43. {
  44. //备份文件
  45. m_MutexSection.WaitOne();
  46. DirectoryInfo dirBak = new DirectoryInfo(strBakDir);
  47. if (!dirBak.Exists)
  48. {
  49. CreateDir(strBakDir);
  50. }
  51. //删除该文件
  52. try
  53. {
  54. //做备份处理
  55. #if TEST
  56. #else
  57. string strText = File.ReadAllText(fname);
  58. strText += fileContext;//带上该帧数据
  59. DelBakFile(MAX_NUMBER_FILE, uFileName, strText);
  60. #endif
  61. File.WriteAllText(fname, "");
  62. }
  63. catch (Exception ex)
  64. {
  65. Console.Write(ex.ToString());
  66. }
  67. m_MutexSection.ReleaseMutex();
  68. //判断备份件个数是否大于N,大于删除最先一个,再备份
  69. }
  70. else
  71. {
  72. try
  73. {
  74. //如果不存在,创建文件,并写内容
  75. if (!finfo.Exists)
  76. {
  77. CreateMyFile(uFileName, fileContext);
  78. }
  79. //否则的话,追加
  80. else
  81. {
  82. AppendMyText(uFileName, fileContext);
  83. }
  84. }
  85. catch (Exception ex)
  86. {
  87. Console.Write(ex.ToString());
  88. }
  89. }
  90. }
  91. //创建文件
  92. /// <summary>
  93. /// 创建新的文件
  94. /// </summary>
  95. /// <param name="uFileName">文件编号</param>
  96. /// <param name="fileContext">要记录的信息</param>
  97. private void CreateMyFile(ushort uFileName, string fileContext)
  98. {
  99. m_MutexSection.WaitOne();
  100. FileInfo fi = new FileInfo(strDir + "\\" + uFileName + ".txt");
  101. //写操作
  102. StreamWriter w = fi.CreateText();
  103. w.WriteLine(fileContext);
  104. w.Close();
  105. m_MutexSection.ReleaseMutex();
  106. }
  107. /// <summary>
  108. /// 追加信息
  109. /// </summary>
  110. /// <param name="uFileName">文件的编号</param>
  111. /// <param name="fileContext">要记录的信息</param>
  112. private void AppendMyText(ushort uFileName, string fileContext)
  113. {
  114. m_MutexSection.WaitOne();
  115. try
  116. {
  117. FileInfo fi = new FileInfo(strDir + "\\" + uFileName + ".txt");
  118. StreamWriter w = fi.AppendText();
  119. w.WriteLine(fileContext);
  120. w.Close();
  121. }
  122. catch (Exception ex)
  123. {
  124. Console.Write(ex.ToString());
  125. }
  126. m_MutexSection.ReleaseMutex();
  127. }
  128. /// <summary>
  129. /// 获取系统时间
  130. /// </summary>
  131. /// <returns>返回系统时间的编码</returns>
  132. private string GetSysTime()
  133. {
  134. string timeFileName = "";
  135. timeFileName = DateTime.Now.Year.ToString();
  136. if (int.Parse(DateTime.Now.Month.ToString()) < 10)
  137. {
  138. timeFileName += "0" + DateTime.Now.Month.ToString();
  139. }
  140. else
  141. {
  142. timeFileName += DateTime.Now.Month.ToString();
  143. }
  144. if (int.Parse(DateTime.Now.Day.ToString()) < 10)
  145. {
  146. timeFileName += "0" + DateTime.Now.Day.ToString();
  147. }
  148. else
  149. {
  150. timeFileName += DateTime.Now.Day.ToString();
  151. }
  152. if (int.Parse(DateTime.Now.Hour.ToString()) < 10)
  153. {
  154. timeFileName += "0" + DateTime.Now.Hour.ToString();
  155. }
  156. else
  157. {
  158. timeFileName += DateTime.Now.Hour.ToString();
  159. }
  160. if (int.Parse(DateTime.Now.Minute.ToString()) < 10)
  161. {
  162. timeFileName += "0" + DateTime.Now.Minute.ToString();
  163. }
  164. else
  165. {
  166. timeFileName += DateTime.Now.Minute.ToString();
  167. }
  168. if (int.Parse(DateTime.Now.Second.ToString()) < 10)
  169. {
  170. timeFileName += "0" + DateTime.Now.Second.ToString();
  171. }
  172. else
  173. {
  174. timeFileName += DateTime.Now.Second.ToString();
  175. }
  176. return timeFileName;
  177. }
  178. //创建目录
  179. private void CreateDir(string directory)
  180. {
  181. DirectoryInfo dirInfo = new DirectoryInfo(directory);
  182. dirInfo.Create();
  183. }
  184. //判断备份文件个数是否大于N,大于删除最先一个,再备份
  185. private void DelBakFile(int n, ushort ID, string strText)
  186. {
  187. mFileList.Clear();
  188. string strPathFile = strBakDir + "\\";
  189. int bakFileNum = GetBakFileNum(ID);
  190. mFileList.Sort();
  191. if (bakFileNum > n)
  192. {
  193. string strMax = strPathFile + mFileList[bakFileNum - 1];
  194. FileInfo finfo = new FileInfo(strMax);
  195. if (finfo.Length > FILE_BACK_LENGTH)
  196. {
  197. strPathFile += mFileList[0];
  198. FileInfo firBakFile = new FileInfo(strPathFile);
  199. firBakFile.Delete();
  200. DelBakFile(n, ID, strText);
  201. }
  202. else
  203. {
  204. strPathFile += mFileList[bakFileNum - 1];
  205. File.AppendAllText(strPathFile, strText);
  206. }
  207. }
  208. else if (bakFileNum == 0)
  209. {
  210. string bakName = strBakDir + "\\";
  211. bakName += GetSysTime();
  212. bakName += "_" + ID;
  213. bakName += "_";
  214. bakName += ".txt";
  215. FileInfo fi = new FileInfo(bakName);
  216. //写操作
  217. StreamWriter w = fi.CreateText();
  218. w.Write(strText);
  219. w.Close();
  220. }
  221. else if (bakFileNum > 0 && bakFileNum <= n)
  222. {
  223. string strMax = strPathFile + mFileList[bakFileNum - 1];
  224. FileInfo finfo = new FileInfo(strMax);
  225. if (finfo.Length > FILE_BACK_LENGTH)
  226. {
  227. string bakName = strBakDir + "\\";
  228. bakName += GetSysTime();
  229. bakName += "_" + ID;
  230. bakName += "_";
  231. bakName += ".txt";
  232. FileInfo firBakFile = new FileInfo(bakName);
  233. StreamWriter w = firBakFile.CreateText();
  234. w.Write(strText);
  235. w.Close();
  236. }
  237. else
  238. {
  239. File.AppendAllText(strMax, strText);
  240. }
  241. }
  242. }
  243. //获得备份文件个数
  244. private int GetBakFileNum(ushort ID)
  245. {
  246. int n = 0;
  247. DirectoryInfo dirInfo = new DirectoryInfo(strBakDir);
  248. string str = "_" + ID.ToString() + "_";
  249. foreach (FileSystemInfo fsi in dirInfo.GetFileSystemInfos())
  250. {
  251. if (fsi is FileInfo && fsi.Extension == ".txt")
  252. {
  253. if (fsi.Name.Contains(str))
  254. {
  255. mFileList.Add(fsi.Name);
  256. n++;
  257. }
  258. }
  259. }
  260. return n;
  261. }
  262. }
  263. }