Form1.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 = @"C:\";
  18. FileWatcher.Filter = "*.jdf";
  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 = 1800000;
  28. timer1.Start();
  29. }
  30. private void Watcher_Created(object sender, FileSystemEventArgs e)
  31. {
  32. if (!e.FullPath.Contains("RECYCLE"))
  33. {
  34. string FullName = e.FullPath;
  35. string Filename = FullName.Substring(FullName.LastIndexOf(@"\") + 1).Split('.')[0];
  36. string StartPath = FullName.Substring(0, FullName.LastIndexOf(@"\") + 1);
  37. if (File.Exists(FullName))
  38. {
  39. richTextBox1.AppendText(DateTime.Now.ToString("yyyy/MM/dd h:mm:ss.fff") + e.FullPath + "\n");
  40. DoLog(FullName);
  41. }
  42. else
  43. {
  44. richTextBox1.AppendText("不存在文件" + FullName + "\n");
  45. }
  46. //不存在同名的文件则进行转换
  47. if (!File.Exists(StartPath + Filename + ".xls") && File.Exists(FullName))
  48. {
  49. exec(@"C:\FileWatcher\DTS-JDFData2Excel.exe", @"C:\FileWatcher\DTS-JDFData2Excel.exe " + FullName);
  50. }
  51. }
  52. }
  53. public void exec(string exePath, string parameters)
  54. {
  55. Process process = new System.Diagnostics.Process();
  56. process.StartInfo.FileName = exePath;
  57. process.StartInfo.Arguments = parameters;
  58. process.StartInfo.UseShellExecute = false;
  59. process.StartInfo.CreateNoWindow = true;
  60. process.StartInfo.RedirectStandardOutput = true;
  61. process.BeginOutputReadLine();
  62. process.OutputDataReceived += new DataReceivedEventHandler(Process_OutputDataReceived);
  63. }
  64. private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
  65. {
  66. }
  67. public void DoLog(string Message)
  68. {
  69. try
  70. {
  71. StreamWriter sw = File.AppendText(DateTime.Now.ToString("yyyy-MM-dd") + ".txt");
  72. sw.WriteLine("\n【时间】" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + Message + "\n");
  73. sw.Close();
  74. }
  75. catch (Exception) { }
  76. }
  77. /// <summary>
  78. /// 私有变量
  79. /// </summary>
  80. private List<FileInfo> lst = new List<FileInfo>();
  81. /// <summary>
  82. /// 获得目录下所有文件或指定文件类型文件(包含所有子文件夹)
  83. /// </summary>
  84. /// <param name="path">文件夹路径</param>
  85. /// <param name="extName">扩展名可以多个 例如 .mp3.wma.rm</param>
  86. /// <returns>List<FileInfo></returns>
  87. public List<FileInfo> getFile(string path, string extName)
  88. {
  89. getdir(path, extName);
  90. return lst;
  91. }
  92. /// <summary>
  93. /// 私有方法,递归获取指定类型文件,包含子文件夹
  94. /// </summary>
  95. /// <param name="path"></param>
  96. /// <param name="extName"></param>
  97. private void getdir(string path, string extName)
  98. {
  99. try
  100. {
  101. string[] dir = Directory.GetDirectories(path); //文件夹列表
  102. DirectoryInfo fdir = new DirectoryInfo(path);
  103. FileInfo[] file = fdir.GetFiles();
  104. //FileInfo[] file = Directory.GetFiles(path); //文件列表
  105. if (file.Length != 0 || dir.Length != 0) //当前目录文件或文件夹不为空
  106. {
  107. foreach (FileInfo f in file) //显示当前目录所有文件
  108. {
  109. if (f.Attributes.ToString().IndexOf("Hidden") > -1 && f.Attributes.ToString().IndexOf("System") > -1)
  110. {
  111. }
  112. else
  113. {
  114. if (extName.ToLower().IndexOf(f.Extension.ToLower()) >= 0)
  115. {
  116. lst.Add(f);
  117. }
  118. }
  119. }
  120. foreach (string d in dir)
  121. {
  122. if (d.Contains("PROB"))
  123. getdir(d, extName);//递归
  124. }
  125. }
  126. }
  127. catch (Exception ex)
  128. {
  129. throw ex;
  130. }
  131. }
  132. private void timer1_Tick(object sender, EventArgs e)
  133. {
  134. Process[] processes1 = Process.GetProcessesByName("DTS-JDFData2Excel");
  135. for (int i = 0; i < processes1.Length; i++)
  136. {
  137. processes1[i].Kill();
  138. }
  139. }
  140. }
  141. }