Form1.cs 16 KB

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