Form1.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Windows.Forms;
  7. namespace FileWatcher
  8. {
  9. public partial class Form1 : Form
  10. {
  11. public Form1()
  12. {
  13. InitializeComponent();
  14. }
  15. private void Form1_Load(object sender, EventArgs e)
  16. {
  17. FileWatcher.Path = @"D:\";
  18. FileWatcher.Filter = "*.*";
  19. FileWatcher.EnableRaisingEvents = true;
  20. FileWatcher.Created += new FileSystemEventHandler(Watcher_Created);
  21. string path = Application.ExecutablePath;
  22. RegistryKey rk = Registry.LocalMachine;
  23. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  24. rk2.SetValue("FileWatcher.exe", path);
  25. rk2.Close();
  26. rk.Close();
  27. timer1.Interval = 600000;
  28. timer1.Start();
  29. }
  30. private void Watcher_Created(object sender, FileSystemEventArgs e)
  31. {
  32. if (e.FullPath.Substring(e.FullPath.LastIndexOf(".") + 1) == "jdf" || e.FullPath.Substring(e.FullPath.LastIndexOf(".") + 1) == "njdf")
  33. {
  34. if (!e.FullPath.Contains("RECYCLE"))
  35. {
  36. string FullName = e.FullPath;
  37. string Filename = FullName.Substring(FullName.LastIndexOf(@"\") + 1).Split('.')[0];
  38. string StartPath = FullName.Substring(0, FullName.LastIndexOf(@"\") + 1);
  39. if (File.Exists(FullName))
  40. {
  41. richTextBox1.AppendText(DateTime.Now.ToString("yyyy/MM/dd h:mm:ss.fff") + e.FullPath + "\n");
  42. DoLog(FullName);
  43. }
  44. else
  45. {
  46. richTextBox1.AppendText("不存在文件" + FullName + "\n");
  47. }
  48. //不存在同名的文件则进行转换
  49. if (!File.Exists(StartPath + Filename + ".xls") && File.Exists(FullName))
  50. {
  51. if (FullName.Substring(FullName.LastIndexOf(".") + 1) == "jdf")
  52. {
  53. exec(@"D:\FileWatcher\JDF\DTS-JDFData2Excel.exe", @"D:\FileWatcher\JDF\DTS-JDFData2Excel.exe " + FullName);
  54. }
  55. else if (FullName.Substring(FullName.LastIndexOf(".") + 1) == "njdf")
  56. {
  57. exec(@"D:\FileWatcher\NJDF\DTS-Data2Excel.exe", @"D:\FileWatcher\NJDF\DTS-Data2Excel.exe " + FullName);
  58. }
  59. }
  60. }
  61. }
  62. }
  63. public void exec(string exePath, string parameters)
  64. {
  65. Process process = new Process();
  66. process.StartInfo.FileName = exePath;
  67. process.StartInfo.Arguments = parameters;
  68. process.StartInfo.UseShellExecute = false;
  69. process.StartInfo.CreateNoWindow = true;
  70. process.StartInfo.RedirectStandardOutput = true;
  71. process.Start();
  72. }
  73. private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
  74. {
  75. }
  76. public void DoLog(string Message)
  77. {
  78. try
  79. {
  80. StreamWriter sw = File.AppendText(DateTime.Now.ToString("yyyy-MM-dd") + ".txt");
  81. sw.WriteLine("\n【时间】" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + Message + "\n");
  82. sw.Close();
  83. }
  84. catch (Exception) { }
  85. }
  86. /// <summary>
  87. /// 私有变量
  88. /// </summary>
  89. private List<FileInfo> lst = new List<FileInfo>();
  90. /// <summary>
  91. /// 获得目录下所有文件或指定文件类型文件(包含所有子文件夹)
  92. /// </summary>
  93. /// <param name="path">文件夹路径</param>
  94. /// <param name="extName">扩展名可以多个 例如 .mp3.wma.rm</param>
  95. /// <returns>List<FileInfo></returns>
  96. public List<FileInfo> getFile(string path, string extName)
  97. {
  98. getdir(path, extName);
  99. return lst;
  100. }
  101. /// <summary>
  102. /// 私有方法,递归获取指定类型文件,包含子文件夹
  103. /// </summary>
  104. /// <param name="path"></param>
  105. /// <param name="extName"></param>
  106. private void getdir(string path, string extName)
  107. {
  108. try
  109. {
  110. string[] dir = Directory.GetDirectories(path); //文件夹列表
  111. DirectoryInfo fdir = new DirectoryInfo(path);
  112. FileInfo[] file = fdir.GetFiles();
  113. //FileInfo[] file = Directory.GetFiles(path); //文件列表
  114. if (file.Length != 0 || dir.Length != 0) //当前目录文件或文件夹不为空
  115. {
  116. foreach (FileInfo f in file) //显示当前目录所有文件
  117. {
  118. if (f.Attributes.ToString().IndexOf("Hidden") > -1 && f.Attributes.ToString().IndexOf("System") > -1)
  119. {
  120. }
  121. else
  122. {
  123. if (extName.ToLower().IndexOf(f.Extension.ToLower()) >= 0)
  124. {
  125. lst.Add(f);
  126. }
  127. }
  128. }
  129. foreach (string d in dir)
  130. {
  131. if (d.Contains("PROB"))
  132. getdir(d, extName);//递归
  133. }
  134. }
  135. }
  136. catch (Exception ex)
  137. {
  138. throw ex;
  139. }
  140. }
  141. private void timer1_Tick(object sender, EventArgs e)
  142. {
  143. Process[] processes1 = Process.GetProcessesByName("DTS-JDFData2Excel");
  144. for (int i = 0; i < processes1.Length; i++)
  145. {
  146. processes1[i].Kill();
  147. }
  148. Process[] processes2 = Process.GetProcessesByName("DTS-Data2Excel");
  149. for (int i = 0; i < processes2.Length; i++)
  150. {
  151. processes2[i].Kill();
  152. }
  153. }
  154. }
  155. }