测试记录解析.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 static List<FileInfo> getFile(string path, string extName, List<FileInfo> lst)
  47. {
  48. try
  49. {
  50. string[] directories = Directory.GetDirectories(path);
  51. DirectoryInfo directoryInfo = new DirectoryInfo(path);
  52. FileInfo[] files = directoryInfo.GetFiles();
  53. if (files.Length != 0 || directories.Length != 0)
  54. {
  55. FileInfo[] array = files;
  56. foreach (FileInfo fileInfo in array)
  57. {
  58. if (extName.ToLower().IndexOf(fileInfo.Extension.ToLower()) >= 0)
  59. {
  60. lst.Add(fileInfo);
  61. }
  62. }
  63. string[] array2 = directories;
  64. foreach (string path2 in array2)
  65. {
  66. getFile(path2, extName, lst);
  67. }
  68. }
  69. return lst;
  70. }
  71. catch (Exception ex)
  72. {
  73. throw ex;
  74. }
  75. }
  76. /// <summary>
  77. /// 私有方法,递归获取指定类型文件,包含子文件夹
  78. /// </summary>
  79. /// <param name="path"></param>
  80. /// <param name="extName"></param>
  81. private void getdir(string path, string extName)
  82. {
  83. try
  84. {
  85. string[] directories = Directory.GetDirectories(path);
  86. DirectoryInfo directoryInfo = new DirectoryInfo(path);
  87. FileInfo[] files = directoryInfo.GetFiles();
  88. if (files.Length != 0 || directories.Length != 0)
  89. {
  90. string text = "";
  91. string text2 = "";
  92. FileInfo[] array = files;
  93. foreach (FileInfo fileInfo in array)
  94. {
  95. text2 = fileInfo.FullName.Substring(0, fileInfo.FullName.LastIndexOf("\\"));
  96. text = text2.Substring(text2.LastIndexOf("\\") + 1);
  97. if (fileInfo.FullName.Contains("SN_"))
  98. {
  99. StreamReader streamReader = new StreamReader(fileInfo.FullName);
  100. while (!streamReader.EndOfStream)
  101. {
  102. string text3 = streamReader.ReadLine();
  103. string text4 = text3.Split(':')[0].Trim();
  104. string text5 = text3.Split(':')[1].Trim();
  105. dh.ExecuteSql("insert into windowsninfo(ws_id,ws_sncode,ws_type,ws_value,ws_indate)values(windowsninfo_seq.nextval,'" + text + "','" + text4 + "','" + text5 + "',sysdate)", "insert");
  106. }
  107. streamReader.Close();
  108. }
  109. try
  110. {
  111. string uploadFolder = "/" + DateTime.Now.ToString("yyyy-MM-dd") + "/" + text2.Substring(text2.LastIndexOf("\\") + 1) + "/";
  112. ftp.UpLoadFile(text2, fileInfo.Name, uploadFolder, BackUpFolderPath.Text);
  113. }
  114. catch (Exception ex)
  115. {
  116. Console.WriteLine(ex.Message);
  117. }
  118. }
  119. FileInfo[] array2 = files;
  120. foreach (FileInfo fileInfo2 in array2)
  121. {
  122. try
  123. {
  124. if (!Directory.Exists(BackUpFolderPath.Text + "\\" + text + "\\"))
  125. {
  126. Directory.CreateDirectory(BackUpFolderPath.Text + "\\" + text + "\\");
  127. }
  128. File.Move(fileInfo2.FullName, BackUpFolderPath.Text + "\\" + text + "\\" + fileInfo2.Name);
  129. }
  130. catch (Exception ex2)
  131. {
  132. Console.WriteLine(ex2.Message);
  133. }
  134. }
  135. try
  136. {
  137. Directory.Delete(text2);
  138. }
  139. catch (Exception)
  140. {
  141. }
  142. string[] array3 = directories;
  143. foreach (string path2 in array3)
  144. {
  145. getdir(path2, extName);
  146. }
  147. }
  148. }
  149. catch (Exception ex4)
  150. {
  151. Console.WriteLine(ex4.Message);
  152. }
  153. }
  154. private void timer1_Tick(object sender, EventArgs e)
  155. {
  156. try
  157. {
  158. }
  159. catch (Exception ex)
  160. {
  161. Console.WriteLine(ex.Message);
  162. }
  163. }
  164. private void ChooseFolder_Click(object sender, EventArgs e)
  165. {
  166. FolderBrowserDialog folder = new FolderBrowserDialog();
  167. folder.Description = "选择监控文件夹";
  168. DialogResult result = folder.ShowDialog();
  169. if (result == DialogResult.OK)
  170. {
  171. FolderPath.Text = folder.SelectedPath;
  172. }
  173. }
  174. private void ChooseBackUpFolder_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. BackUpFolderPath.Text = folder.SelectedPath;
  182. }
  183. }
  184. private void Start_Click(object sender, EventArgs e)
  185. {
  186. timer1.Start();
  187. }
  188. }
  189. }