测试记录解析.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Windows.Forms;
  10. namespace FileWatcher
  11. {
  12. public partial class 测试记录解析 : Form
  13. {
  14. DataHelper dh = new DataHelper();
  15. public 测试记录解析()
  16. {
  17. InitializeComponent();
  18. }
  19. private void Form1_Load(object sender, EventArgs e)
  20. {
  21. timer1.Interval = 1000 * 10;
  22. timer1.Start();
  23. }
  24. public void DoLog(string Message)
  25. {
  26. try
  27. {
  28. StreamWriter sw = File.AppendText(DateTime.Now.ToString("yyyy-MM-dd") + ".txt");
  29. sw.WriteLine("\n【时间】" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + Message + "\n");
  30. sw.Close();
  31. }
  32. catch (Exception) { }
  33. }
  34. /// <summary>
  35. /// 私有变量
  36. /// </summary>
  37. private List<FileInfo> lst = new List<FileInfo>();
  38. /// <summary>
  39. /// 获得目录下所有文件或指定文件类型文件(包含所有子文件夹)
  40. /// </summary>
  41. /// <param name="path">文件夹路径</param>
  42. /// <param name="extName">扩展名可以多个 例如 .mp3.wma.rm</param>
  43. /// <returns>List<FileInfo></returns>
  44. public List<FileInfo> getFile(string path, string extName)
  45. {
  46. getdir(path, extName);
  47. return lst;
  48. }
  49. /// <summary>
  50. /// 私有方法,递归获取指定类型文件,包含子文件夹
  51. /// </summary>
  52. /// <param name="path"></param>
  53. /// <param name="extName"></param>
  54. private void getdir(string path, string extName)
  55. {
  56. try
  57. {
  58. string[] dir = Directory.GetDirectories(path); //文件夹列表
  59. DirectoryInfo fdir = new DirectoryInfo(path);
  60. FileInfo[] file = fdir.GetFiles();
  61. //FileInfo[] file = Directory.GetFiles(path); //文件列表
  62. if (file.Length != 0 || dir.Length != 0) //当前目录文件或文件夹不为空
  63. {
  64. foreach (FileInfo f in file) //显示当前目录所有文件
  65. {
  66. if (f.FullName.Contains("SN_"))
  67. {
  68. Console.WriteLine(f.FullName);
  69. StreamReader sr = new StreamReader(f.FullName);
  70. while (!sr.EndOfStream)
  71. {
  72. string val = sr.ReadLine();
  73. string type = val.Split(':')[0].Trim();
  74. string value = val.Split(':')[1].Trim();
  75. dh.ExecuteSql("insert into windowsninfo(ws_id,ws_sncode,ws_type,ws_value)values(windowsninfo_seq.nextval,'" + f.Name + "','"+type+"','"+value+"')", "insert");
  76. }
  77. sr.Close();
  78. }
  79. //if (f.Attributes.ToString().IndexOf("Hidden") > -1 && f.Attributes.ToString().IndexOf("System") > -1)
  80. //{
  81. //}
  82. //else
  83. //{
  84. // if (extName.ToLower().IndexOf(f.Extension.ToLower()) >= 0)
  85. // {
  86. // lst.Add(f);
  87. // }
  88. //}
  89. }
  90. foreach (string d in dir)
  91. {
  92. Console.WriteLine(d);
  93. getdir(d, extName);//递归
  94. }
  95. }
  96. }
  97. catch (Exception ex)
  98. {
  99. throw ex;
  100. }
  101. }
  102. private void timer1_Tick(object sender, EventArgs e)
  103. {
  104. getFile(FolderPath.Text, ".log");
  105. }
  106. /// <summary>
  107. /// 获得目录下所有文件或指定文件类型文件(包含所有子文件夹)
  108. /// </summary>
  109. /// <param name="path">文件夹路径</param>
  110. /// <param name="extName">扩展名可以多个 例如 .mp3.wma.rm</param>
  111. /// <returns>List<FileInfo></returns>
  112. public static List<FileInfo> getFile(string path, string extName, List<FileInfo> lst)
  113. {
  114. try
  115. {
  116. string[] dir = Directory.GetDirectories(path); //文件夹列表
  117. DirectoryInfo fdir = new DirectoryInfo(path);
  118. FileInfo[] file = fdir.GetFiles();
  119. //FileInfo[] file = Directory.GetFiles(path); //文件列表
  120. if (file.Length != 0 || dir.Length != 0) //当前目录文件或文件夹不为空
  121. {
  122. foreach (FileInfo f in file) //显示当前目录所有文件
  123. {
  124. if (extName.ToLower().IndexOf(f.Extension.ToLower()) >= 0)
  125. {
  126. lst.Add(f);
  127. }
  128. }
  129. foreach (string d in dir)
  130. {
  131. getFile(d, extName, lst);//递归
  132. }
  133. }
  134. return lst;
  135. }
  136. catch (Exception ex)
  137. {
  138. throw ex;
  139. }
  140. }
  141. private void ChooseFolder_Click(object sender, EventArgs e)
  142. {
  143. FolderBrowserDialog folder = new FolderBrowserDialog();
  144. folder.Description = "选择监控文件夹";
  145. DialogResult result = folder.ShowDialog();
  146. if (result == DialogResult.OK)
  147. {
  148. FolderPath.Text = folder.SelectedPath;
  149. }
  150. }
  151. private void ChooseBackUpFolder_Click(object sender, EventArgs e)
  152. {
  153. FolderBrowserDialog folder = new FolderBrowserDialog();
  154. folder.Description = "选择备份文件夹";
  155. DialogResult result = folder.ShowDialog();
  156. if (result == DialogResult.OK)
  157. {
  158. BackUpFolderPath.Text = folder.SelectedPath;
  159. }
  160. }
  161. private void Start_Click(object sender, EventArgs e)
  162. {
  163. timer1.Start();
  164. }
  165. }
  166. }