测试记录解析DCW.cs 6.9 KB

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