Form1.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. RunTran();
  33. }
  34. private void Watcher_Created(object sender, FileSystemEventArgs e)
  35. {
  36. if (e.FullPath.Substring(e.FullPath.LastIndexOf(".") + 1) == "jdf" || e.FullPath.Substring(e.FullPath.LastIndexOf(".") + 1) == "njdf")
  37. {
  38. if (!e.FullPath.Contains("RECYCLE"))
  39. {
  40. string FullName = e.FullPath;
  41. string Filename = FullName.Substring(FullName.LastIndexOf(@"\") + 1).Split('.')[0];
  42. string StartPath = FullName.Substring(0, FullName.LastIndexOf(@"\") + 1);
  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. //不存在同名的文件则进行转换
  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. Process[] processes1 = Process.GetProcessesByName("DTS-JDFData2Excel");
  152. for (int i = 0; i < processes1.Length; i++)
  153. {
  154. //杀掉超过20秒没关闭的进程
  155. if (DateTime.Now > processes1[1].StartTime.AddSeconds(20))
  156. {
  157. processes1[i].Kill();
  158. }
  159. }
  160. Process[] processes2 = Process.GetProcessesByName("DTS-Data2Excel");
  161. for (int i = 0; i < processes2.Length; i++)
  162. {
  163. if (DateTime.Now > processes2[1].StartTime.AddSeconds(20))
  164. {
  165. processes2[i].Kill();
  166. }
  167. }
  168. }
  169. private void RunTran()
  170. {
  171. try
  172. {
  173. List<FileInfo> lst = new List<FileInfo>();
  174. string[] path = new string[] { @"D:\PROB-001", @"D:\PROB-002", @"D:\PROB-003", @"D:\PROB-004",
  175. @"D:\PROB-005", @"D:\PROB-006", @"D:\PROB-007", @"D:\PROB-008", @"D:\PROB-009",
  176. @"D:\PROB-010", @"D:\PROB-011", @"D:\PROB-012", @"D:\PROB-013", @"D:\PROB-014",
  177. @"D:\PROB-015", @"D:\PROB-004", @"D:\PROB-016", @"D:\PROB-017", @"D:\PROB-018",
  178. @"D:\PROB-019", @"D:\PROB-020", @"D:\PROB-021", @"D:\PROB-022", @"D:\PROB-023",
  179. @"D:\PROB-024", @"D:\PROB-026",@"D:\PROB-027",@"D:\PROB-028",@"D:\PROB-029",
  180. @"D:\PROB-030",@"D:\PROB-031",@"D:\PROB-032",@"D:\PROB-033",@"D:\PROB-034",
  181. @"D:\PROB-035",@"D:\PROB-036",@"D:\PROB-037",@"D:\PROB-038",@"D:\PROB-039",
  182. @"D:\PROB-040",@"D:\PROB-041",@"D:\PROB-042",@"D:\PROB-043",@"D:\PROB-045",@"D:\PROB-046",
  183. @"D:\PROB-047",@"D:\PROB-048",@"D:\PROB-049",@"D:\PROB-050",@"D:\PROB-051",@"D:\PROB-052",@"D:\PROB-053",
  184. @"D:\PROB-054",@"D:\PROB-055",@"D:\PROB-056",@"D:\PROB-057",@"D:\PROB-058",@"D:\PROB-059",@"D:\PROB-060",
  185. @"D:\PROB-061",@"D:\PROB-062",@"D:\PROB-063",@"D:\PROB-064",@"D:\PROB-65",@"D:\PROB-66",@"D:\PROB-067",
  186. @"D:\PROB-068",@"D:\PROB-069",@"D:\PROB-070",@"D:\PROB-071",@"D:\PROB-072",@"D:\PROB-073",@"D:\PROB-074",
  187. @"D:\PROB-075",@"D:\PROB-76",@"D:\PROB-077",@"D:\PROB-078",@"D:\PROB-079",@"D:\PROB-080",@"D:\PROB-081",@"D:\PROB-082"};
  188. //string[] path = textBox1.Text.Split(',');
  189. for (int i = 0; i < path.Length; i++)
  190. {
  191. richTextBox1.AppendText(path[i] + "\n");
  192. List<FileInfo> lstFiles = getFile(path[i], ".jdf", lst);
  193. foreach (FileInfo shpFile in lstFiles)
  194. {
  195. string Filename = shpFile.FullName.Substring(shpFile.FullName.LastIndexOf(@"\") + 1).Split('.')[0];
  196. string StartPath = shpFile.FullName.Substring(0, shpFile.FullName.LastIndexOf(@"\") + 1);
  197. //不存在同名的文件则进行转换
  198. if (!File.Exists(StartPath + Filename + ".xls"))
  199. {
  200. Thread.Sleep(1000);
  201. exec(@"D:\FileWatcher\JDF\DTS-JDFData2Excel.exe", @"D:\FileWatcher\JDF\DTS-JDFData2Excel.exe " + shpFile.FullName);
  202. richTextBox1.AppendText(shpFile.FullName + "\n");
  203. }
  204. }
  205. }
  206. }
  207. catch (Exception ex)
  208. {
  209. richTextBox1.AppendText(ex.Message + ex.StackTrace + "\n");
  210. }
  211. }
  212. /// <summary>
  213. /// 获得目录下所有文件或指定文件类型文件(包含所有子文件夹)
  214. /// </summary>
  215. /// <param name="path">文件夹路径</param>
  216. /// <param name="extName">扩展名可以多个 例如 .mp3.wma.rm</param>
  217. /// <returns>List<FileInfo></returns>
  218. public static List<FileInfo> getFile(string path, string extName, List<FileInfo> lst)
  219. {
  220. try
  221. {
  222. string[] dir = Directory.GetDirectories(path); //文件夹列表
  223. DirectoryInfo fdir = new DirectoryInfo(path);
  224. FileInfo[] file = fdir.GetFiles();
  225. //FileInfo[] file = Directory.GetFiles(path); //文件列表
  226. if (file.Length != 0 || dir.Length != 0) //当前目录文件或文件夹不为空
  227. {
  228. foreach (FileInfo f in file) //显示当前目录所有文件
  229. {
  230. if (extName.ToLower().IndexOf(f.Extension.ToLower()) >= 0)
  231. {
  232. lst.Add(f);
  233. }
  234. }
  235. foreach (string d in dir)
  236. {
  237. getFile(d, extName, lst);//递归
  238. }
  239. }
  240. return lst;
  241. }
  242. catch (Exception ex)
  243. {
  244. throw ex;
  245. }
  246. }
  247. private void timer2_Tick(object sender, EventArgs e)
  248. {
  249. RunTran();
  250. }
  251. }
  252. }