Form1.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. using HslCommunication;
  2. using Microsoft.Win32;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Diagnostics;
  7. using System.IO;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Windows.Forms;
  11. namespace FileWatcher
  12. {
  13. public partial class Form1 : Form
  14. {
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. }
  19. private void Form1_Load(object sender, EventArgs e)
  20. {
  21. HslCommunication.Profinet.Panasonic.PanasonicMewtocol plc = new HslCommunication.Profinet.Panasonic.PanasonicMewtocol();
  22. plc.Station = 238;
  23. HslCommunication.Core.Pipe.PipeSerialPort pipe = new HslCommunication.Core.Pipe.PipeSerialPort();
  24. pipe.SerialPortInni("COM12-9600-8-O-1");
  25. pipe.RtsEnable = false;
  26. pipe.DtrEnable = false;
  27. pipe.SleepTime = 20;
  28. pipe.ReceiveTimeOut = 5000;
  29. plc.CommunicationPipe = pipe;
  30. // 当前读取操作的代码 The code for the current read operation
  31. OperateResult<short> read = plc.ReadInt16("D60");
  32. if (read.IsSuccess)
  33. {
  34. Console.WriteLine("Read [D60] Success, Value: " + read.Content);
  35. }
  36. else
  37. {
  38. Console.WriteLine("Read [D60] failed: " + read.Message);
  39. }
  40. // 当前写入操作的代码 The code for the current write operation
  41. OperateResult write = plc.Write("D60", short.Parse("2"));
  42. if (write.IsSuccess)
  43. {
  44. Console.WriteLine("Write [D60] success");
  45. }
  46. else
  47. {
  48. Console.WriteLine("Write [D60] failed: " + write.Message);
  49. }
  50. }
  51. private void Watcher_Created(object sender, FileSystemEventArgs e)
  52. {
  53. if (e.FullPath.Substring(e.FullPath.LastIndexOf(".") + 1) == "jdf" || e.FullPath.Substring(e.FullPath.LastIndexOf(".") + 1) == "njdf")
  54. {
  55. if (!e.FullPath.Contains("RECYCLE"))
  56. {
  57. string FullName = e.FullPath;
  58. string Filename = FullName.Substring(FullName.LastIndexOf(@"\") + 1).Split('.')[0];
  59. string StartPath = FullName.Substring(0, FullName.LastIndexOf(@"\") + 1);
  60. Thread.Sleep(1500);
  61. if (File.Exists(FullName))
  62. {
  63. richTextBox1.AppendText(DateTime.Now.ToString("yyyy/MM/dd h:mm:ss.fff") + " " + e.FullPath + "\n");
  64. DoLog(FullName);
  65. }
  66. else
  67. {
  68. richTextBox1.AppendText("不存在文件" + FullName + "\n");
  69. }
  70. //不存在同名的文件则进行转换,或者Excel文件小于100KB的
  71. if ((!File.Exists(StartPath + Filename + ".xls") && File.Exists(FullName)))
  72. {
  73. if (FullName.Substring(FullName.LastIndexOf(".") + 1) == "jdf")
  74. {
  75. exec(@"D:\FileWatcher\JDF\DTS-JDFData2Excel.exe", @"D:\FileWatcher\JDF\DTS-JDFData2Excel.exe " + FullName);
  76. }
  77. else if (FullName.Substring(FullName.LastIndexOf(".") + 1) == "njdf")
  78. {
  79. exec(@"D:\FileWatcher\NJDF\DTS-Data2Excel.exe", @"D:\FileWatcher\NJDF\DTS-Data2Excel.exe " + FullName);
  80. }
  81. }
  82. else
  83. {
  84. richTextBox1.AppendText(DateTime.Now.ToString("yyyy/MM/dd h:mm:ss.fff") + " 已存在Excel文件 " + e.FullPath + "\n");
  85. }
  86. }
  87. }
  88. }
  89. public void exec(string exePath, string parameters)
  90. {
  91. Process process = new Process();
  92. process.StartInfo.FileName = exePath;
  93. process.StartInfo.Arguments = parameters;
  94. process.StartInfo.UseShellExecute = false;
  95. process.StartInfo.CreateNoWindow = true;
  96. process.StartInfo.RedirectStandardOutput = true;
  97. process.Start();
  98. }
  99. private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
  100. {
  101. }
  102. public void DoLog(string Message)
  103. {
  104. try
  105. {
  106. StreamWriter sw = File.AppendText(DateTime.Now.ToString("yyyy-MM-dd") + ".txt");
  107. sw.WriteLine("\n【时间】" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + Message + "\n");
  108. sw.Close();
  109. }
  110. catch (Exception) { }
  111. }
  112. /// <summary>
  113. /// 私有变量
  114. /// </summary>
  115. private List<FileInfo> lst = new List<FileInfo>();
  116. /// <summary>
  117. /// 获得目录下所有文件或指定文件类型文件(包含所有子文件夹)
  118. /// </summary>
  119. /// <param name="path">文件夹路径</param>
  120. /// <param name="extName">扩展名可以多个 例如 .mp3.wma.rm</param>
  121. /// <returns>List<FileInfo></returns>
  122. public List<FileInfo> getFile(string path, string extName)
  123. {
  124. getdir(path, extName);
  125. return lst;
  126. }
  127. /// <summary>
  128. /// 私有方法,递归获取指定类型文件,包含子文件夹
  129. /// </summary>
  130. /// <param name="path"></param>
  131. /// <param name="extName"></param>
  132. private void getdir(string path, string extName)
  133. {
  134. try
  135. {
  136. string[] dir = Directory.GetDirectories(path); //文件夹列表
  137. DirectoryInfo fdir = new DirectoryInfo(path);
  138. FileInfo[] file = fdir.GetFiles();
  139. //FileInfo[] file = Directory.GetFiles(path); //文件列表
  140. if (file.Length != 0 || dir.Length != 0) //当前目录文件或文件夹不为空
  141. {
  142. foreach (FileInfo f in file) //显示当前目录所有文件
  143. {
  144. if (f.Attributes.ToString().IndexOf("Hidden") > -1 && f.Attributes.ToString().IndexOf("System") > -1)
  145. {
  146. }
  147. else
  148. {
  149. if (extName.ToLower().IndexOf(f.Extension.ToLower()) >= 0)
  150. {
  151. lst.Add(f);
  152. }
  153. }
  154. }
  155. foreach (string d in dir)
  156. {
  157. if (d.Contains("PROB"))
  158. getdir(d, extName);//递归
  159. }
  160. }
  161. }
  162. catch (Exception ex)
  163. {
  164. throw ex;
  165. }
  166. }
  167. private void timer1_Tick(object sender, EventArgs e)
  168. {
  169. try
  170. {
  171. Process[] processes1 = Process.GetProcessesByName("DTS-JDFData2Excel");
  172. for (int i = 0; i < processes1.Length; i++)
  173. {
  174. //杀掉超过20秒没关闭的进程
  175. if (DateTime.Now > processes1[1].StartTime.AddSeconds(60))
  176. {
  177. processes1[i].Kill();
  178. }
  179. }
  180. Process[] processes2 = Process.GetProcessesByName("DTS-Data2Excel");
  181. for (int i = 0; i < processes2.Length; i++)
  182. {
  183. if (DateTime.Now > processes2[1].StartTime.AddSeconds(60))
  184. {
  185. processes2[i].Kill();
  186. }
  187. }
  188. }
  189. catch (Exception)
  190. {
  191. }
  192. //每天0点清空文本框的内容
  193. if (DateTime.Now.ToString("HH:mm") == "00:00")
  194. {
  195. richTextBox1.Clear();
  196. }
  197. }
  198. private void RunTran()
  199. {
  200. try
  201. {
  202. List<FileInfo> lst = new List<FileInfo>();
  203. string[] path = new string[] { @"D:\PROB-071",@"D:\PROB-072",@"D:\PROB-073",@"D:\PROB-074",
  204. @"D:\PROB-075",@"D:\PROB-76",@"D:\PROB-077",@"D:\PROB-078",@"D:\PROB-079",@"D:\PROB-080",@"D:\PROB-081",@"D:\PROB-082",@"D:\PROB-001", @"D:\PROB-002", @"D:\PROB-003", @"D:\PROB-004",
  205. @"D:\PROB-005", @"D:\PROB-006", @"D:\PROB-007", @"D:\PROB-008", @"D:\PROB-009",
  206. @"D:\PROB-010", @"D:\PROB-011", @"D:\PROB-012", @"D:\PROB-013", @"D:\PROB-014",
  207. @"D:\PROB-015", @"D:\PROB-004", @"D:\PROB-016", @"D:\PROB-017", @"D:\PROB-018",
  208. @"D:\PROB-019", @"D:\PROB-020", @"D:\PROB-021", @"D:\PROB-022", @"D:\PROB-023",
  209. @"D:\PROB-024", @"D:\PROB-026",@"D:\PROB-027",@"D:\PROB-028",@"D:\PROB-029",
  210. @"D:\PROB-030",@"D:\PROB-031",@"D:\PROB-032",@"D:\PROB-033",@"D:\PROB-034",
  211. @"D:\PROB-035",@"D:\PROB-036",@"D:\PROB-037",@"D:\PROB-038",@"D:\PROB-039",
  212. @"D:\PROB-040",@"D:\PROB-041",@"D:\PROB-042",@"D:\PROB-043",@"D:\PROB-045",@"D:\PROB-046",
  213. @"D:\PROB-047",@"D:\PROB-048",@"D:\PROB-049",@"D:\PROB-050",@"D:\PROB-051",@"D:\PROB-052",@"D:\PROB-053",
  214. @"D:\PROB-054",@"D:\PROB-055",@"D:\PROB-056",@"D:\PROB-057",@"D:\PROB-058",@"D:\PROB-059",@"D:\PROB-060",
  215. @"D:\PROB-061",@"D:\PROB-062",@"D:\PROB-063",@"D:\PROB-064",@"D:\PROB-65",@"D:\PROB-66",@"D:\PROB-067",
  216. @"D:\PROB-068",@"D:\PROB-069",@"D:\PROB-070"};
  217. for (int i = 0; i < path.Length; i++)
  218. {
  219. richTextBox1.AppendText(path[i] + "\n");
  220. List<FileInfo> lstFiles = getFile(path[i], ".jdf", lst);
  221. foreach (FileInfo shpFile in lstFiles)
  222. {
  223. string Filename = shpFile.FullName.Substring(shpFile.FullName.LastIndexOf(@"\") + 1).Split('.')[0];
  224. string StartPath = shpFile.FullName.Substring(0, shpFile.FullName.LastIndexOf(@"\") + 1);
  225. //不存在同名的文件则进行转换
  226. if (!File.Exists(StartPath + Filename + ".xls"))
  227. {
  228. Thread.Sleep(1500);
  229. exec(@"D:\FileWatcher\JDF\DTS-JDFData2Excel.exe", @"D:\FileWatcher\JDF\DTS-JDFData2Excel.exe " + shpFile.FullName);
  230. richTextBox1.AppendText(shpFile.FullName + "\n");
  231. }
  232. }
  233. lst.Clear();
  234. List<FileInfo> lstFiles1 = getFile(path[i], ".njdf", lst);
  235. foreach (FileInfo shpFile in lstFiles1)
  236. {
  237. string Filename = shpFile.FullName.Substring(shpFile.FullName.LastIndexOf(@"\") + 1).Split('.')[0];
  238. string StartPath = shpFile.FullName.Substring(0, shpFile.FullName.LastIndexOf(@"\") + 1);
  239. //不存在同名的文件则进行转换
  240. if (!File.Exists(StartPath + Filename + ".xls"))
  241. {
  242. Thread.Sleep(1500);
  243. exec(@"D:\FileWatcher\NJDF\DTS-Data2Excel.exe", @"D:\FileWatcher\NJDF\DTS-Data2Excel.exe " + shpFile.FullName);
  244. richTextBox1.AppendText(shpFile.FullName + "\n");
  245. }
  246. }
  247. }
  248. }
  249. catch (Exception ex)
  250. {
  251. richTextBox1.AppendText(ex.Message + ex.StackTrace + "\n");
  252. }
  253. }
  254. private void RunTran1()
  255. {
  256. try
  257. {
  258. List<FileInfo> lst = new List<FileInfo>();
  259. string[] path = textBox1.Text.Split(',');
  260. for (int i = 0; i < path.Length; i++)
  261. {
  262. richTextBox1.AppendText(path[i] + "\n");
  263. List<FileInfo> lstFiles = getFile(path[i], ".jdf", lst);
  264. foreach (FileInfo shpFile in lstFiles)
  265. {
  266. string Filename = shpFile.FullName.Substring(shpFile.FullName.LastIndexOf(@"\") + 1).Split('.')[0];
  267. string StartPath = shpFile.FullName.Substring(0, shpFile.FullName.LastIndexOf(@"\") + 1);
  268. //不存在同名的文件则进行转换
  269. try
  270. {
  271. if (!File.Exists(StartPath + Filename + ".xls") ||
  272. ((new FileInfo(shpFile.FullName).Length / 1024 > 50) && (File.Exists(StartPath + Filename + ".xls") && (new FileInfo(StartPath + Filename + ".xls").Length / 1024) < 100)))
  273. {
  274. Thread.Sleep(1500);
  275. exec(@"D:\FileWatcher\JDF\DTS-JDFData2Excel.exe", @"D:\FileWatcher\JDF\DTS-JDFData2Excel.exe " + shpFile.FullName);
  276. richTextBox1.AppendText(shpFile.FullName + "\n");
  277. }
  278. }
  279. catch (Exception ex)
  280. {
  281. richTextBox1.AppendText(ex.Message + ex.StackTrace + "\n");
  282. }
  283. }
  284. lst.Clear();
  285. List<FileInfo> lstFiles1 = getFile(path[i], ".njdf", lst);
  286. foreach (FileInfo shpFile in lstFiles1)
  287. {
  288. string Filename = shpFile.FullName.Substring(shpFile.FullName.LastIndexOf(@"\") + 1).Split('.')[0];
  289. string StartPath = shpFile.FullName.Substring(0, shpFile.FullName.LastIndexOf(@"\") + 1);
  290. //不存在同名的文件则进行转换
  291. try
  292. {
  293. Thread.Sleep(1500);
  294. exec(@"D:\FileWatcher\NJDF\DTS-Data2Excel.exe", @"D:\FileWatcher\NJDF\DTS-Data2Excel.exe " + shpFile.FullName);
  295. richTextBox1.AppendText(shpFile.FullName + "\n");
  296. }
  297. catch (Exception ex)
  298. {
  299. richTextBox1.AppendText(ex.Message + ex.StackTrace + "\n");
  300. }
  301. }
  302. }
  303. }
  304. catch (Exception ex)
  305. {
  306. richTextBox1.AppendText(ex.Message + ex.StackTrace + "\n");
  307. }
  308. }
  309. /// <summary>
  310. /// 获得目录下所有文件或指定文件类型文件(包含所有子文件夹)
  311. /// </summary>
  312. /// <param name="path">文件夹路径</param>
  313. /// <param name="extName">扩展名可以多个 例如 .mp3.wma.rm</param>
  314. /// <returns>List<FileInfo></returns>
  315. public static List<FileInfo> getFile(string path, string extName, List<FileInfo> lst)
  316. {
  317. try
  318. {
  319. string[] dir = Directory.GetDirectories(path); //文件夹列表
  320. DirectoryInfo fdir = new DirectoryInfo(path);
  321. FileInfo[] file = fdir.GetFiles();
  322. //FileInfo[] file = Directory.GetFiles(path); //文件列表
  323. if (file.Length != 0 || dir.Length != 0) //当前目录文件或文件夹不为空
  324. {
  325. foreach (FileInfo f in file) //显示当前目录所有文件
  326. {
  327. if (extName.ToLower().IndexOf(f.Extension.ToLower()) >= 0)
  328. {
  329. lst.Add(f);
  330. }
  331. }
  332. foreach (string d in dir)
  333. {
  334. getFile(d, extName, lst);//递归
  335. }
  336. }
  337. return lst;
  338. }
  339. catch (Exception ex)
  340. {
  341. throw ex;
  342. }
  343. }
  344. private void timer2_Tick(object sender, EventArgs e)
  345. {
  346. RunTran();
  347. }
  348. private void RunTran2()
  349. {
  350. DataHelper dh = new DataHelper();
  351. StringBuilder sql = new StringBuilder();
  352. sql.Clear();
  353. sql.Append("select chipcode_,replace(replace(replace(substr(JDFPATH_,instr(JDFPATH_,'$')-1),'$',':'),'/','\\'),'\\\\','\\') jdf ");
  354. sql.Append(" from datacenter$chip where chipcode_ in (select A_meschip.CHIPCODE from A_meschip) and datacenter$chip.JDFPATH_ is not null");
  355. DataTable dt = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  356. for (int i = 0; i < dt.Rows.Count; i++)
  357. {
  358. string FullName = dt.Rows[i]["jdf"].ToString();
  359. if (FullName.Substring(FullName.LastIndexOf(".") + 1) == "jdf")
  360. {
  361. exec(@"D:\FileWatcher\JDF\DTS-JDFData2Excel.exe", @"D:\FileWatcher\JDF\DTS-JDFData2Excel.exe " + FullName);
  362. }
  363. else if (FullName.Substring(FullName.LastIndexOf(".") + 1) == "njdf")
  364. {
  365. exec(@"D:\FileWatcher\NJDF\DTS-Data2Excel.exe", @"D:\FileWatcher\NJDF\DTS-Data2Excel.exe " + FullName);
  366. }
  367. richTextBox1.AppendText(FullName + "\n");
  368. }
  369. }
  370. private void button1_Click(object sender, EventArgs e)
  371. {
  372. Thread tr = new Thread(RunTran1);
  373. tr.Start();
  374. }
  375. }
  376. }