Form1.cs 17 KB

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