CBlackBoxFile.cs 9.0 KB

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