测试记录解析.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. ftpOperater ftp = new ftpOperater();
  16. public 测试记录解析()
  17. {
  18. InitializeComponent();
  19. }
  20. private void Form1_Load(object sender, EventArgs e)
  21. {
  22. timer1.Interval = 1000 * 10;
  23. OperateResult.AppendText("连接上服务器\n");
  24. //timer1.Start();
  25. }
  26. public void DoLog(string Message)
  27. {
  28. try
  29. {
  30. StreamWriter sw = File.AppendText(DateTime.Now.ToString("yyyy-MM-dd") + ".txt");
  31. sw.WriteLine("\n【时间】" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + Message + "\n");
  32. sw.Close();
  33. }
  34. catch (Exception) { }
  35. }
  36. /// <summary>
  37. /// 私有变量
  38. /// </summary>
  39. private List<FileInfo> lst = new List<FileInfo>();
  40. /// <summary>
  41. /// 获得目录下所有文件或指定文件类型文件(包含所有子文件夹)
  42. /// </summary>
  43. /// <param name="path">文件夹路径</param>
  44. /// <param name="extName">扩展名可以多个 例如 .mp3.wma.rm</param>
  45. /// <returns>List<FileInfo></returns>
  46. public List<FileInfo> getFile(string path, string extName)
  47. {
  48. getdir(path, extName);
  49. return lst;
  50. }
  51. /// <summary>
  52. /// 私有方法,递归获取指定类型文件,包含子文件夹
  53. /// </summary>
  54. /// <param name="path"></param>
  55. /// <param name="extName"></param>
  56. private void getdir(string path, string extName)
  57. {
  58. try
  59. {
  60. string[] dir = Directory.GetDirectories(path); //文件夹列表
  61. DirectoryInfo fdir = new DirectoryInfo(path);
  62. FileInfo[] file = fdir.GetFiles();
  63. //FileInfo[] file = Directory.GetFiles(path); //文件列表
  64. if (file.Length != 0 || dir.Length != 0) //当前目录文件或文件夹不为空
  65. {
  66. string SN = "";
  67. string folderpath = "";
  68. foreach (FileInfo f in file) //显示当前目录所有文件
  69. {
  70. folderpath = f.FullName.Substring(0, f.FullName.LastIndexOf(@"\"));
  71. SN = folderpath.Substring(folderpath.LastIndexOf(@"\") + 1);
  72. if (f.FullName.Contains("SN_"))
  73. {
  74. StreamReader sr = new StreamReader(f.FullName);
  75. while (!sr.EndOfStream)
  76. {
  77. string val = sr.ReadLine();
  78. string type = val.Split(':')[0].Trim();
  79. string value = val.Split(':')[1].Trim();
  80. dh.ExecuteSql("insert into windowsninfo(ws_id,ws_sncode,ws_type,ws_value,ws_indate)values(windowsninfo_seq.nextval,'" + SN + "','" + type + "','" + value + "',sysdate)", "insert");
  81. }
  82. sr.Close();
  83. }
  84. try
  85. {
  86. string ftppath = "/" + DateTime.Now.ToString("yyyy-MM-dd") + "/" + folderpath.Substring(folderpath.LastIndexOf(@"\") + 1) + "/";
  87. ftp.UpLoadFile(folderpath, f.Name, ftppath, BackUpFolderPath.Text);
  88. }
  89. catch (Exception ex)
  90. {
  91. Console.WriteLine(ex.Message);
  92. }
  93. }
  94. foreach (FileInfo f in file) //显示当前目录所有文件
  95. {
  96. try
  97. {
  98. if (!Directory.Exists(BackUpFolderPath.Text + @"\" + SN + @"\"))
  99. {
  100. Directory.CreateDirectory(BackUpFolderPath.Text + @"\" + SN + @"\");
  101. }
  102. File.Move(f.FullName, BackUpFolderPath.Text + @"\" + SN + @"\" + f.Name);
  103. }
  104. catch (Exception ex)
  105. {
  106. Console.WriteLine(ex.Message);
  107. }
  108. }
  109. try
  110. {
  111. Directory.Delete(folderpath);
  112. }
  113. catch (Exception)
  114. {
  115. }
  116. foreach (string d in dir)
  117. {
  118. getdir(d, extName);//递归
  119. }
  120. }
  121. }
  122. catch (Exception ex)
  123. {
  124. Console.WriteLine(ex.Message);
  125. }
  126. }
  127. private void timer1_Tick(object sender, EventArgs e)
  128. {
  129. try
  130. {
  131. getFile(FolderPath.Text, ".log");
  132. }
  133. catch (Exception ex)
  134. {
  135. Console.WriteLine(ex.Message);
  136. }
  137. }
  138. /// <summary>
  139. /// 获得目录下所有文件或指定文件类型文件(包含所有子文件夹)
  140. /// </summary>
  141. /// <param name="path">文件夹路径</param>
  142. /// <param name="extName">扩展名可以多个 例如 .mp3.wma.rm</param>
  143. /// <returns>List<FileInfo></returns>
  144. public static List<FileInfo> getFile(string path, string extName, List<FileInfo> lst)
  145. {
  146. try
  147. {
  148. string[] dir = Directory.GetDirectories(path); //文件夹列表
  149. DirectoryInfo fdir = new DirectoryInfo(path);
  150. FileInfo[] file = fdir.GetFiles();
  151. //FileInfo[] file = Directory.GetFiles(path); //文件列表
  152. if (file.Length != 0 || dir.Length != 0) //当前目录文件或文件夹不为空
  153. {
  154. foreach (FileInfo f in file) //显示当前目录所有文件
  155. {
  156. if (extName.ToLower().IndexOf(f.Extension.ToLower()) >= 0)
  157. {
  158. lst.Add(f);
  159. }
  160. }
  161. foreach (string d in dir)
  162. {
  163. getFile(d, extName, lst);//递归
  164. }
  165. }
  166. return lst;
  167. }
  168. catch (Exception ex)
  169. {
  170. throw ex;
  171. }
  172. }
  173. private void ChooseFolder_Click(object sender, EventArgs e)
  174. {
  175. FolderBrowserDialog folder = new FolderBrowserDialog();
  176. folder.Description = "选择监控文件夹";
  177. DialogResult result = folder.ShowDialog();
  178. if (result == DialogResult.OK)
  179. {
  180. FolderPath.Text = folder.SelectedPath;
  181. }
  182. }
  183. private void ChooseBackUpFolder_Click(object sender, EventArgs e)
  184. {
  185. FolderBrowserDialog folder = new FolderBrowserDialog();
  186. folder.Description = "选择备份文件夹";
  187. DialogResult result = folder.ShowDialog();
  188. if (result == DialogResult.OK)
  189. {
  190. BackUpFolderPath.Text = folder.SelectedPath;
  191. }
  192. }
  193. private void Start_Click(object sender, EventArgs e)
  194. {
  195. timer1.Start();
  196. }
  197. }
  198. }