测试记录解析DCW.cs 7.3 KB

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