测试记录解析DCW.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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() == "INI")
  73. {
  74. folderpath = f.FullName.Substring(0, f.FullName.LastIndexOf(@"\"));
  75. SN = f.Name.Split('.')[0].ToUpper().Split('_')[0];
  76. //if (SN.Length == 12)
  77. //{
  78. try
  79. {
  80. string ftppath = "/" + DateTime.Now.ToString("yyyy-MM-dd") + "/";
  81. ftp.UpLoadFile(folderpath, f.Name, ftppath, "");
  82. int dt = int.Parse(dh.ExecuteSql("insert into STEPTESTDETAIL (std_id,std_sn,std_makecode,std_indate,std_class)values( STEPTESTDETAIL_seq.nextval,'" + SN + "','" + ma_code.Text + "',sysdate,'http://81.71.42.91:8099/ftp" + ftppath + f.Name + "')", "insert").ToString());
  83. if (dt > 0)
  84. {
  85. OperateResult.AppendText("序列号:" + SN + "上传成功\n");
  86. }
  87. //else
  88. //{
  89. // OperateResult.AppendText("序列号: " + SN + "前工段未扫描\n");
  90. // MessageBox.Show("序列号: " + SN + "前工段未扫描", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
  91. //}
  92. }
  93. catch (Exception ex)
  94. {
  95. Console.WriteLine(ex.Message);
  96. }
  97. File.Delete(f.FullName);
  98. //}
  99. }
  100. }
  101. }
  102. }
  103. catch (Exception ex)
  104. {
  105. Console.WriteLine(ex.Message);
  106. }
  107. }
  108. private void timer1_Tick(object sender, EventArgs e)
  109. {
  110. try
  111. {
  112. getFile(FolderPath.Text, ".txt");
  113. }
  114. catch (Exception ex)
  115. {
  116. Console.WriteLine(ex.Message);
  117. }
  118. }
  119. /// <summary>
  120. /// 获得目录下所有文件或指定文件类型文件(包含所有子文件夹)
  121. /// </summary>
  122. /// <param name="path">文件夹路径</param>
  123. /// <param name="extName">扩展名可以多个 例如 .mp3.wma.rm</param>
  124. /// <returns>List<FileInfo></returns>
  125. public static List<FileInfo> getFile(string path, string extName, List<FileInfo> lst)
  126. {
  127. try
  128. {
  129. string[] dir = Directory.GetDirectories(path); //文件夹列表
  130. DirectoryInfo fdir = new DirectoryInfo(path);
  131. FileInfo[] file = fdir.GetFiles();
  132. //FileInfo[] file = Directory.GetFiles(path); //文件列表
  133. if (file.Length != 0 || dir.Length != 0) //当前目录文件或文件夹不为空
  134. {
  135. foreach (FileInfo f in file) //显示当前目录所有文件
  136. {
  137. if (extName.ToLower().IndexOf(f.Extension.ToLower()) >= 0)
  138. {
  139. lst.Add(f);
  140. }
  141. }
  142. foreach (string d in dir)
  143. {
  144. getFile(d, extName, lst);//递归
  145. }
  146. }
  147. return lst;
  148. }
  149. catch (Exception ex)
  150. {
  151. throw ex;
  152. }
  153. }
  154. private void ChooseFolder_Click(object sender, EventArgs e)
  155. {
  156. FolderBrowserDialog folder = new FolderBrowserDialog();
  157. folder.Description = "选择监控文件夹";
  158. DialogResult result = folder.ShowDialog();
  159. if (result == DialogResult.OK)
  160. {
  161. FolderPath.Text = folder.SelectedPath;
  162. }
  163. }
  164. private void ChooseBackUpFolder_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. // BackUpFolderPath.Text = folder.SelectedPath;
  172. }
  173. }
  174. private void Start_Click(object sender, EventArgs e)
  175. {
  176. if (!dh.CheckExist("make", "ma_code='" + ma_code.Text + "'"))
  177. {
  178. OperateResult.AppendText("工单" + ma_code.Text + "不存在\n");
  179. return;
  180. }
  181. timer1.Start();
  182. }
  183. }
  184. }