Form1.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 = "*.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. }
  28. private void Watcher_Created(object sender, FileSystemEventArgs e)
  29. {
  30. using (var process = new Process())
  31. {
  32. if (!e.FullPath.Contains("RECYCLE"))
  33. {
  34. string FullName = e.FullPath;
  35. process.StartInfo.Arguments = @"D:\FileWatcher\DTS-JDFData2Excel.exe " + FullName;
  36. string Filename = FullName.Substring(FullName.LastIndexOf(@"\") + 1).Split('.')[0];
  37. string StartPath = FullName.Substring(0, FullName.LastIndexOf(@"\") + 1);
  38. if (File.Exists(FullName))
  39. {
  40. richTextBox1.AppendText(DateTime.Now.ToString("yyyy/MM/dd h:mm:ss.fff") + e.FullPath + "\n");
  41. }
  42. else
  43. {
  44. richTextBox1.AppendText("不存在文件" + FullName + "\n");
  45. }
  46. //不存在同名的文件则进行转换
  47. if (!File.Exists(StartPath + Filename + ".xls") && File.Exists(FullName))
  48. {
  49. process.StartInfo.FileName = @"D:\FileWatcher\DTS-JDFData2Excel.exe";
  50. process.StartInfo.UseShellExecute = false;
  51. process.StartInfo.RedirectStandardInput = true;
  52. process.StartInfo.RedirectStandardOutput = true;
  53. process.StartInfo.RedirectStandardError = true;
  54. process.StartInfo.CreateNoWindow = true;
  55. process.Start();
  56. process.StandardInput.AutoFlush = true;
  57. process.StandardInput.WriteLine("exit");
  58. //获取cmd窗口的输出信息
  59. string output = process.StandardOutput.ReadToEnd();
  60. process.WaitForExit();
  61. process.Close();
  62. }
  63. }
  64. }
  65. }
  66. /// <summary>
  67. /// 私有变量
  68. /// </summary>
  69. private List<FileInfo> lst = new List<FileInfo>();
  70. /// <summary>
  71. /// 获得目录下所有文件或指定文件类型文件(包含所有子文件夹)
  72. /// </summary>
  73. /// <param name="path">文件夹路径</param>
  74. /// <param name="extName">扩展名可以多个 例如 .mp3.wma.rm</param>
  75. /// <returns>List<FileInfo></returns>
  76. public List<FileInfo> getFile(string path, string extName)
  77. {
  78. getdir(path, extName);
  79. return lst;
  80. }
  81. /// <summary>
  82. /// 私有方法,递归获取指定类型文件,包含子文件夹
  83. /// </summary>
  84. /// <param name="path"></param>
  85. /// <param name="extName"></param>
  86. private void getdir(string path, string extName)
  87. {
  88. try
  89. {
  90. string[] dir = Directory.GetDirectories(path); //文件夹列表
  91. DirectoryInfo fdir = new DirectoryInfo(path);
  92. FileInfo[] file = fdir.GetFiles();
  93. //FileInfo[] file = Directory.GetFiles(path); //文件列表
  94. if (file.Length != 0 || dir.Length != 0) //当前目录文件或文件夹不为空
  95. {
  96. foreach (FileInfo f in file) //显示当前目录所有文件
  97. {
  98. if (f.Attributes.ToString().IndexOf("Hidden") > -1 && f.Attributes.ToString().IndexOf("System") > -1)
  99. {
  100. }
  101. else
  102. {
  103. if (extName.ToLower().IndexOf(f.Extension.ToLower()) >= 0)
  104. {
  105. lst.Add(f);
  106. }
  107. }
  108. }
  109. foreach (string d in dir)
  110. {
  111. if (d.Contains("PROB"))
  112. getdir(d, extName);//递归
  113. }
  114. }
  115. }
  116. catch (Exception ex)
  117. {
  118. throw ex;
  119. }
  120. }
  121. }
  122. }