TxtFile.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace TeamAAS_VP.Core
  8. {
  9. public class TxtFile
  10. {
  11. private string _filepath = string.Empty;
  12. private string _filename = string.Empty;
  13. private int _filesize = 0;
  14. private int _interval = 0;
  15. private string _seperator = ",";
  16. private int logcount = 0;
  17. private bool _bWrite = true;
  18. private object _objlock = new object();
  19. public bool bWrite
  20. {
  21. get
  22. {
  23. return _bWrite;
  24. }
  25. set
  26. {
  27. _bWrite = value;
  28. }
  29. }
  30. public string FilePath
  31. {
  32. get
  33. {
  34. return _filepath;
  35. }
  36. set
  37. {
  38. _filepath = value;
  39. }
  40. }
  41. public string FileName
  42. {
  43. get
  44. {
  45. return _filename;
  46. }
  47. set
  48. {
  49. _filename = value;
  50. }
  51. }
  52. public int FileSize
  53. {
  54. get
  55. {
  56. return _filesize;
  57. }
  58. set
  59. {
  60. _filesize = value;
  61. }
  62. }
  63. public int Interval
  64. {
  65. get
  66. {
  67. return _interval;
  68. }
  69. set
  70. {
  71. if (value > 0 && value <= 12)
  72. {
  73. _interval = value;
  74. }
  75. else if (value < 0)
  76. {
  77. _interval = 0;
  78. }
  79. else
  80. {
  81. _interval = 12;
  82. }
  83. }
  84. }
  85. public string Seperator
  86. {
  87. get
  88. {
  89. return _seperator;
  90. }
  91. set
  92. {
  93. _seperator = value;
  94. }
  95. }
  96. public TxtFile()
  97. {
  98. }
  99. public TxtFile(string strFilePath, string strFileName)
  100. {
  101. _filepath = strFilePath;
  102. _filename = strFileName;
  103. }
  104. public TxtFile(string strFilePath, string strFileName, int filesize, int interval, string seperator)
  105. {
  106. _filepath = strFilePath;
  107. _filename = strFileName;
  108. _filesize = filesize;
  109. _interval = interval;
  110. _seperator = seperator;
  111. }
  112. public void WriteLine(string strSave)
  113. {
  114. if (!bWrite)
  115. {
  116. return;
  117. }
  118. try
  119. {
  120. lock (_objlock)
  121. {
  122. if (!Directory.Exists(_filepath))
  123. {
  124. Directory.CreateDirectory(_filepath);
  125. }
  126. string text = DateTime.Now.ToString("yyyy-MM-dd-");
  127. if (_interval > 0 && _interval <= 12)
  128. {
  129. logcount = DateTime.Now.Hour / _interval;
  130. }
  131. string text2 = _filepath + "\\" + _filename;
  132. string text3 = Path.GetExtension(text2);
  133. if (text3.Length == 0)
  134. {
  135. text2 += ".log";
  136. text3 = ".log";
  137. }
  138. string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(text2);
  139. if (!File.Exists(_filepath + "\\" + fileNameWithoutExtension + text + logcount + text3))
  140. {
  141. using (File.Create(_filepath + "\\" + fileNameWithoutExtension + text + logcount + text3))
  142. {
  143. }
  144. }
  145. using (StreamWriter streamWriter = File.AppendText(_filepath + "\\" + fileNameWithoutExtension + text + logcount + text3))
  146. {
  147. StringBuilder stringBuilder = new StringBuilder();
  148. stringBuilder.Append(text.Substring(0, text.Length - 1));
  149. stringBuilder.Append(_seperator);
  150. string value = DateTime.Now.TimeOfDay.ToString();
  151. stringBuilder.Append(value);
  152. stringBuilder.Append(_seperator);
  153. stringBuilder.Append(strSave);
  154. if (strSave.Length > 2)
  155. {
  156. if ('\r' != strSave[strSave.Length - 2] || '\n' != strSave[strSave.Length - 1])
  157. {
  158. streamWriter.WriteLine(stringBuilder.ToString());
  159. }
  160. else
  161. {
  162. streamWriter.Write(stringBuilder.ToString());
  163. }
  164. }
  165. else
  166. {
  167. streamWriter.WriteLine(stringBuilder.ToString());
  168. }
  169. streamWriter.Flush();
  170. streamWriter.Close();
  171. }
  172. if (_filesize > 0)
  173. {
  174. FileInfo fileInfo = new FileInfo(_filepath + fileNameWithoutExtension + "\\" + text + logcount + text3);
  175. if (fileInfo.Length > 1048576 * _filesize)
  176. {
  177. logcount++;
  178. }
  179. }
  180. }
  181. }
  182. catch (Exception ex)
  183. {
  184. throw new Exception(ex.Message);
  185. }
  186. }
  187. public void WriteLine(string strSave, bool bwrite)
  188. {
  189. if (bwrite)
  190. {
  191. WriteLine(strSave);
  192. }
  193. }
  194. public void WriteTxt(string strSave)
  195. {
  196. try
  197. {
  198. lock (_objlock)
  199. {
  200. if (!Directory.Exists(_filepath))
  201. {
  202. Directory.CreateDirectory(_filepath);
  203. }
  204. string text = _filepath + "\\" + _filename;
  205. string text2 = Path.GetExtension(text);
  206. if (text2.Length == 0)
  207. {
  208. text += ".txt";
  209. text2 = ".txt";
  210. }
  211. string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(text);
  212. if (!File.Exists(_filepath + "\\" + fileNameWithoutExtension + text2))
  213. {
  214. using (File.Create(_filepath + "\\" + fileNameWithoutExtension + text2))
  215. {
  216. }
  217. }
  218. StreamWriter streamWriter = File.AppendText(_filepath + "\\" + fileNameWithoutExtension + text2);
  219. StringBuilder stringBuilder = new StringBuilder();
  220. stringBuilder.Append(strSave);
  221. if (strSave.Length > 2)
  222. {
  223. if ('\r' != strSave[strSave.Length - 2] || '\n' != strSave[strSave.Length - 1])
  224. {
  225. streamWriter.WriteLine(stringBuilder.ToString());
  226. }
  227. else
  228. {
  229. streamWriter.Write(stringBuilder.ToString());
  230. }
  231. }
  232. else
  233. {
  234. streamWriter.WriteLine(stringBuilder.ToString());
  235. }
  236. streamWriter.Flush();
  237. streamWriter.Close();
  238. }
  239. }
  240. catch (Exception ex)
  241. {
  242. throw new Exception(ex.Message);
  243. }
  244. }
  245. public void WriteTxtAndName(string strSave, string filename)
  246. {
  247. try
  248. {
  249. lock (_objlock)
  250. {
  251. if (!Directory.Exists(_filepath))
  252. {
  253. Directory.CreateDirectory(_filepath);
  254. }
  255. string text = Path.GetExtension(filename);
  256. if (text.Length == 0)
  257. {
  258. filename += ".txt";
  259. text = ".txt";
  260. }
  261. string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filename);
  262. if (!File.Exists(_filepath + "\\" + fileNameWithoutExtension + text))
  263. {
  264. using (File.Create(_filepath + "\\" + fileNameWithoutExtension + text))
  265. {
  266. }
  267. }
  268. StreamWriter streamWriter = File.AppendText(_filepath + "\\" + fileNameWithoutExtension + text);
  269. StringBuilder stringBuilder = new StringBuilder();
  270. stringBuilder.Append(strSave);
  271. if (strSave.Length > 2)
  272. {
  273. if ('\r' != strSave[strSave.Length - 2] || '\n' != strSave[strSave.Length - 1])
  274. {
  275. streamWriter.WriteLine(stringBuilder.ToString());
  276. }
  277. else
  278. {
  279. streamWriter.Write(stringBuilder.ToString());
  280. }
  281. }
  282. else
  283. {
  284. streamWriter.WriteLine(stringBuilder.ToString());
  285. }
  286. streamWriter.Flush();
  287. streamWriter.Close();
  288. }
  289. }
  290. catch (Exception ex)
  291. {
  292. throw new Exception(ex.Message);
  293. }
  294. }
  295. public void Write(string strSave, string strSeperator)
  296. {
  297. StreamWriter streamWriter = null;
  298. string path = _filepath + "\\" + _filename;
  299. try
  300. {
  301. if (!Directory.Exists(_filepath))
  302. {
  303. Directory.CreateDirectory(_filepath);
  304. }
  305. if (!File.Exists(path))
  306. {
  307. using (File.Create(path))
  308. {
  309. }
  310. }
  311. streamWriter = new StreamWriter(path, append: true);
  312. StringBuilder stringBuilder = new StringBuilder();
  313. stringBuilder.Append(strSave);
  314. stringBuilder.Append(strSeperator);
  315. streamWriter.Write(stringBuilder);
  316. streamWriter.Close();
  317. }
  318. catch (Exception ex)
  319. {
  320. throw new Exception(ex.Message);
  321. }
  322. finally
  323. {
  324. streamWriter?.Close();
  325. }
  326. }
  327. public string ReadLine()
  328. {
  329. StreamReader streamReader = null;
  330. try
  331. {
  332. string text = null;
  333. streamReader = new StreamReader(FilePath + "\\" + FileName, detectEncodingFromByteOrderMarks: false);
  334. text = streamReader.ReadLine();
  335. streamReader.Close();
  336. return text;
  337. }
  338. catch (Exception ex)
  339. {
  340. throw new Exception(ex.Message);
  341. }
  342. finally
  343. {
  344. streamReader?.Close();
  345. }
  346. }
  347. public List<string> ReadAll()
  348. {
  349. StreamReader streamReader = null;
  350. try
  351. {
  352. List<string> list = new List<string>();
  353. string text = null;
  354. streamReader = new StreamReader(FilePath + "\\" + FileName, Encoding.GetEncoding("gb2312"), detectEncodingFromByteOrderMarks: false);
  355. for (text = streamReader.ReadLine(); text != null; text = streamReader.ReadLine())
  356. {
  357. list.Add(text);
  358. }
  359. streamReader.Close();
  360. return list;
  361. }
  362. catch (Exception ex)
  363. {
  364. throw new Exception(ex.Message);
  365. }
  366. finally
  367. {
  368. streamReader?.Close();
  369. }
  370. }
  371. }
  372. }