Form1.cs 15 KB

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