测试记录解析.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. OperateResult.AppendText(f.Name + "解析成功");
  94. }
  95. foreach (FileInfo f in file) //显示当前目录所有文件
  96. {
  97. try
  98. {
  99. if (!Directory.Exists(BackUpFolderPath.Text + @"\" + SN + @"\"))
  100. {
  101. Directory.CreateDirectory(BackUpFolderPath.Text + @"\" + SN + @"\");
  102. }
  103. File.Move(f.FullName, BackUpFolderPath.Text + @"\" + SN + @"\" + f.Name);
  104. }
  105. catch (Exception ex)
  106. {
  107. Console.WriteLine(ex.Message);
  108. }
  109. }
  110. try
  111. {
  112. Directory.Delete(folderpath);
  113. }
  114. catch (Exception)
  115. {
  116. }
  117. foreach (string d in dir)
  118. {
  119. getdir(d, extName);//递归
  120. }
  121. }
  122. }
  123. catch (Exception ex)
  124. {
  125. Console.WriteLine(ex.Message);
  126. }
  127. }
  128. private void timer1_Tick(object sender, EventArgs e)
  129. {
  130. try
  131. {
  132. getFile(FolderPath.Text, ".log");
  133. }
  134. catch (Exception ex)
  135. {
  136. Console.WriteLine(ex.Message);
  137. }
  138. }
  139. /// <summary>
  140. /// 获得目录下所有文件或指定文件类型文件(包含所有子文件夹)
  141. /// </summary>
  142. /// <param name="path">文件夹路径</param>
  143. /// <param name="extName">扩展名可以多个 例如 .mp3.wma.rm</param>
  144. /// <returns>List<FileInfo></returns>
  145. public static List<FileInfo> getFile(string path, string extName, List<FileInfo> lst)
  146. {
  147. try
  148. {
  149. string[] dir = Directory.GetDirectories(path); //文件夹列表
  150. DirectoryInfo fdir = new DirectoryInfo(path);
  151. FileInfo[] file = fdir.GetFiles();
  152. //FileInfo[] file = Directory.GetFiles(path); //文件列表
  153. if (file.Length != 0 || dir.Length != 0) //当前目录文件或文件夹不为空
  154. {
  155. foreach (FileInfo f in file) //显示当前目录所有文件
  156. {
  157. if (extName.ToLower().IndexOf(f.Extension.ToLower()) >= 0)
  158. {
  159. lst.Add(f);
  160. }
  161. }
  162. foreach (string d in dir)
  163. {
  164. getFile(d, extName, lst);//递归
  165. }
  166. }
  167. return lst;
  168. }
  169. catch (Exception ex)
  170. {
  171. throw ex;
  172. }
  173. }
  174. private void ChooseFolder_Click(object sender, EventArgs e)
  175. {
  176. FolderBrowserDialog folder = new FolderBrowserDialog();
  177. folder.Description = "选择监控文件夹";
  178. DialogResult result = folder.ShowDialog();
  179. if (result == DialogResult.OK)
  180. {
  181. FolderPath.Text = folder.SelectedPath;
  182. }
  183. }
  184. private void ChooseBackUpFolder_Click(object sender, EventArgs e)
  185. {
  186. FolderBrowserDialog folder = new FolderBrowserDialog();
  187. folder.Description = "选择备份文件夹";
  188. DialogResult result = folder.ShowDialog();
  189. if (result == DialogResult.OK)
  190. {
  191. BackUpFolderPath.Text = folder.SelectedPath;
  192. }
  193. }
  194. private void Start_Click(object sender, EventArgs e)
  195. {
  196. if (Start.Text == "开始监控")
  197. {
  198. timer1.Start();
  199. Start.Text = "停止监控";
  200. }
  201. else
  202. {
  203. timer1.Stop();
  204. Start.Text = "开始监控";
  205. }
  206. }
  207. }
  208. }