Form1.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. }
  177. private void RunTran()
  178. {
  179. try
  180. {
  181. List<FileInfo> lst = new List<FileInfo>();
  182. string[] path = new string[] { @"D:\PROB-071",@"D:\PROB-072",@"D:\PROB-073",@"D:\PROB-074",
  183. @"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",
  184. @"D:\PROB-005", @"D:\PROB-006", @"D:\PROB-007", @"D:\PROB-008", @"D:\PROB-009",
  185. @"D:\PROB-010", @"D:\PROB-011", @"D:\PROB-012", @"D:\PROB-013", @"D:\PROB-014",
  186. @"D:\PROB-015", @"D:\PROB-004", @"D:\PROB-016", @"D:\PROB-017", @"D:\PROB-018",
  187. @"D:\PROB-019", @"D:\PROB-020", @"D:\PROB-021", @"D:\PROB-022", @"D:\PROB-023",
  188. @"D:\PROB-024", @"D:\PROB-026",@"D:\PROB-027",@"D:\PROB-028",@"D:\PROB-029",
  189. @"D:\PROB-030",@"D:\PROB-031",@"D:\PROB-032",@"D:\PROB-033",@"D:\PROB-034",
  190. @"D:\PROB-035",@"D:\PROB-036",@"D:\PROB-037",@"D:\PROB-038",@"D:\PROB-039",
  191. @"D:\PROB-040",@"D:\PROB-041",@"D:\PROB-042",@"D:\PROB-043",@"D:\PROB-045",@"D:\PROB-046",
  192. @"D:\PROB-047",@"D:\PROB-048",@"D:\PROB-049",@"D:\PROB-050",@"D:\PROB-051",@"D:\PROB-052",@"D:\PROB-053",
  193. @"D:\PROB-054",@"D:\PROB-055",@"D:\PROB-056",@"D:\PROB-057",@"D:\PROB-058",@"D:\PROB-059",@"D:\PROB-060",
  194. @"D:\PROB-061",@"D:\PROB-062",@"D:\PROB-063",@"D:\PROB-064",@"D:\PROB-65",@"D:\PROB-66",@"D:\PROB-067",
  195. @"D:\PROB-068",@"D:\PROB-069",@"D:\PROB-070"};
  196. for (int i = 0; i < path.Length; i++)
  197. {
  198. richTextBox1.AppendText(path[i] + "\n");
  199. List<FileInfo> lstFiles = getFile(path[i], ".jdf", lst);
  200. foreach (FileInfo shpFile in lstFiles)
  201. {
  202. string Filename = shpFile.FullName.Substring(shpFile.FullName.LastIndexOf(@"\") + 1).Split('.')[0];
  203. string StartPath = shpFile.FullName.Substring(0, shpFile.FullName.LastIndexOf(@"\") + 1);
  204. //不存在同名的文件则进行转换
  205. if (!File.Exists(StartPath + Filename + ".xls"))
  206. {
  207. Thread.Sleep(1500);
  208. exec(@"D:\FileWatcher\JDF\DTS-JDFData2Excel.exe", @"D:\FileWatcher\JDF\DTS-JDFData2Excel.exe " + shpFile.FullName);
  209. richTextBox1.AppendText(shpFile.FullName + "\n");
  210. }
  211. }
  212. lst.Clear();
  213. List<FileInfo> lstFiles1 = getFile(path[i], ".njdf", lst);
  214. foreach (FileInfo shpFile in lstFiles1)
  215. {
  216. string Filename = shpFile.FullName.Substring(shpFile.FullName.LastIndexOf(@"\") + 1).Split('.')[0];
  217. string StartPath = shpFile.FullName.Substring(0, shpFile.FullName.LastIndexOf(@"\") + 1);
  218. //不存在同名的文件则进行转换
  219. if (!File.Exists(StartPath + Filename + ".xls"))
  220. {
  221. Thread.Sleep(1500);
  222. exec(@"D:\FileWatcher\NJDF\DTS-Data2Excel.exe", @"D:\FileWatcher\NJDF\DTS-Data2Excel.exe " + shpFile.FullName);
  223. richTextBox1.AppendText(shpFile.FullName + "\n");
  224. }
  225. }
  226. }
  227. }
  228. catch (Exception ex)
  229. {
  230. richTextBox1.AppendText(ex.Message + ex.StackTrace + "\n");
  231. }
  232. }
  233. private void RunTran1()
  234. {
  235. try
  236. {
  237. List<FileInfo> lst = new List<FileInfo>();
  238. string[] path = textBox1.Text.Split(',');
  239. for (int i = 0; i < path.Length; i++)
  240. {
  241. richTextBox1.AppendText(path[i] + "\n");
  242. List<FileInfo> lstFiles = getFile(path[i], ".jdf", lst);
  243. foreach (FileInfo shpFile in lstFiles)
  244. {
  245. string Filename = shpFile.FullName.Substring(shpFile.FullName.LastIndexOf(@"\") + 1).Split('.')[0];
  246. string StartPath = shpFile.FullName.Substring(0, shpFile.FullName.LastIndexOf(@"\") + 1);
  247. //不存在同名的文件则进行转换
  248. try
  249. {
  250. if (!File.Exists(StartPath + Filename + ".xls") ||
  251. ((new FileInfo(shpFile.FullName).Length / 1024 > 50)&&(File.Exists(StartPath + Filename + ".xls") && (new FileInfo(StartPath + Filename + ".xls").Length / 1024) < 100)))
  252. {
  253. Thread.Sleep(1500);
  254. exec(@"D:\FileWatcher\JDF\DTS-JDFData2Excel.exe", @"D:\FileWatcher\JDF\DTS-JDFData2Excel.exe " + shpFile.FullName);
  255. richTextBox1.AppendText(shpFile.FullName + "\n");
  256. }
  257. }
  258. catch (Exception ex)
  259. {
  260. richTextBox1.AppendText(ex.Message + ex.StackTrace + "\n");
  261. }
  262. }
  263. lst.Clear();
  264. List<FileInfo> lstFiles1 = getFile(path[i], ".njdf", lst);
  265. foreach (FileInfo shpFile in lstFiles1)
  266. {
  267. string Filename = shpFile.FullName.Substring(shpFile.FullName.LastIndexOf(@"\") + 1).Split('.')[0];
  268. string StartPath = shpFile.FullName.Substring(0, shpFile.FullName.LastIndexOf(@"\") + 1);
  269. //不存在同名的文件则进行转换
  270. try
  271. {
  272. Thread.Sleep(1500);
  273. exec(@"D:\FileWatcher\NJDF\DTS-Data2Excel.exe", @"D:\FileWatcher\NJDF\DTS-Data2Excel.exe " + shpFile.FullName);
  274. richTextBox1.AppendText(shpFile.FullName + "\n");
  275. }
  276. catch (Exception ex)
  277. {
  278. richTextBox1.AppendText(ex.Message + ex.StackTrace + "\n");
  279. }
  280. }
  281. }
  282. }
  283. catch (Exception ex)
  284. {
  285. richTextBox1.AppendText(ex.Message + ex.StackTrace + "\n");
  286. }
  287. }
  288. /// <summary>
  289. /// 获得目录下所有文件或指定文件类型文件(包含所有子文件夹)
  290. /// </summary>
  291. /// <param name="path">文件夹路径</param>
  292. /// <param name="extName">扩展名可以多个 例如 .mp3.wma.rm</param>
  293. /// <returns>List<FileInfo></returns>
  294. public static List<FileInfo> getFile(string path, string extName, List<FileInfo> lst)
  295. {
  296. try
  297. {
  298. string[] dir = Directory.GetDirectories(path); //文件夹列表
  299. DirectoryInfo fdir = new DirectoryInfo(path);
  300. FileInfo[] file = fdir.GetFiles();
  301. //FileInfo[] file = Directory.GetFiles(path); //文件列表
  302. if (file.Length != 0 || dir.Length != 0) //当前目录文件或文件夹不为空
  303. {
  304. foreach (FileInfo f in file) //显示当前目录所有文件
  305. {
  306. if (extName.ToLower().IndexOf(f.Extension.ToLower()) >= 0)
  307. {
  308. lst.Add(f);
  309. }
  310. }
  311. foreach (string d in dir)
  312. {
  313. getFile(d, extName, lst);//递归
  314. }
  315. }
  316. return lst;
  317. }
  318. catch (Exception ex)
  319. {
  320. throw ex;
  321. }
  322. }
  323. private void timer2_Tick(object sender, EventArgs e)
  324. {
  325. RunTran();
  326. }
  327. private void RunTran2()
  328. {
  329. DataHelper dh = new DataHelper();
  330. StringBuilder sql = new StringBuilder();
  331. sql.Clear();
  332. sql.Append("select chipcode_,replace(replace(replace(substr(JDFPATH_,instr(JDFPATH_,'$')-1),'$',':'),'/','\\'),'\\\\','\\') jdf ");
  333. sql.Append(" from datacenter$chip where chipcode_ in (select A_meschip.CHIPCODE from A_meschip) and datacenter$chip.JDFPATH_ is not null");
  334. DataTable dt = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  335. for (int i = 0; i < dt.Rows.Count; i++)
  336. {
  337. string FullName = dt.Rows[i]["jdf"].ToString();
  338. if (FullName.Substring(FullName.LastIndexOf(".") + 1) == "jdf")
  339. {
  340. exec(@"D:\FileWatcher\JDF\DTS-JDFData2Excel.exe", @"D:\FileWatcher\JDF\DTS-JDFData2Excel.exe " + FullName);
  341. }
  342. else if (FullName.Substring(FullName.LastIndexOf(".") + 1) == "njdf")
  343. {
  344. exec(@"D:\FileWatcher\NJDF\DTS-Data2Excel.exe", @"D:\FileWatcher\NJDF\DTS-Data2Excel.exe " + FullName);
  345. }
  346. richTextBox1.AppendText(FullName + "\n");
  347. }
  348. }
  349. private void button1_Click(object sender, EventArgs e)
  350. {
  351. Thread tr = new Thread(RunTran1);
  352. tr.Start();
  353. }
  354. }
  355. }