| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- using Microsoft.Win32;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Windows.Forms;
- namespace FileWatcher
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- FileWatcher.Path = @"D:\";
- FileWatcher.Filter = "*.jdf";
- FileWatcher.EnableRaisingEvents = true;
- FileWatcher.Created += new FileSystemEventHandler(Watcher_Created);
- string path = Application.ExecutablePath;
- RegistryKey rk = Registry.LocalMachine;
- RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
- rk2.SetValue("FileWatcher.exe", path);
- rk2.Close();
- rk.Close();
- }
- private void Watcher_Created(object sender, FileSystemEventArgs e)
- {
- using (var process = new Process())
- {
- if (!e.FullPath.Contains("RECYCLE"))
- {
- string FullName = e.FullPath;
- process.StartInfo.Arguments = @"D:\FileWatcher\DTS-JDFData2Excel.exe " + FullName;
- string Filename = FullName.Substring(FullName.LastIndexOf(@"\") + 1).Split('.')[0];
- string StartPath = FullName.Substring(0, FullName.LastIndexOf(@"\") + 1);
- if (File.Exists(FullName))
- {
- richTextBox1.AppendText(DateTime.Now.ToString("yyyy/MM/dd h:mm:ss.fff") + e.FullPath + "\n");
- }
- else
- {
- richTextBox1.AppendText("不存在文件" + FullName + "\n");
- }
- //不存在同名的文件则进行转换
- if (!File.Exists(StartPath + Filename + ".xls") && File.Exists(FullName))
- {
- process.StartInfo.FileName = @"D:\FileWatcher\DTS-JDFData2Excel.exe";
- process.StartInfo.UseShellExecute = false;
- process.StartInfo.RedirectStandardInput = true;
- process.StartInfo.RedirectStandardOutput = true;
- process.StartInfo.RedirectStandardError = true;
- process.StartInfo.CreateNoWindow = true;
- process.Start();
- process.StandardInput.AutoFlush = true;
- process.StandardInput.WriteLine("exit");
- //获取cmd窗口的输出信息
- string output = process.StandardOutput.ReadToEnd();
- process.WaitForExit();
- process.Close();
- }
- }
- }
- }
- /// <summary>
- /// 私有变量
- /// </summary>
- private List<FileInfo> lst = new List<FileInfo>();
- /// <summary>
- /// 获得目录下所有文件或指定文件类型文件(包含所有子文件夹)
- /// </summary>
- /// <param name="path">文件夹路径</param>
- /// <param name="extName">扩展名可以多个 例如 .mp3.wma.rm</param>
- /// <returns>List<FileInfo></returns>
- public List<FileInfo> getFile(string path, string extName)
- {
- getdir(path, extName);
- return lst;
- }
- /// <summary>
- /// 私有方法,递归获取指定类型文件,包含子文件夹
- /// </summary>
- /// <param name="path"></param>
- /// <param name="extName"></param>
- private void getdir(string path, string extName)
- {
- try
- {
- string[] dir = Directory.GetDirectories(path); //文件夹列表
- DirectoryInfo fdir = new DirectoryInfo(path);
- FileInfo[] file = fdir.GetFiles();
- //FileInfo[] file = Directory.GetFiles(path); //文件列表
- if (file.Length != 0 || dir.Length != 0) //当前目录文件或文件夹不为空
- {
- foreach (FileInfo f in file) //显示当前目录所有文件
- {
- if (f.Attributes.ToString().IndexOf("Hidden") > -1 && f.Attributes.ToString().IndexOf("System") > -1)
- {
- }
- else
- {
- if (extName.ToLower().IndexOf(f.Extension.ToLower()) >= 0)
- {
- lst.Add(f);
- }
- }
- }
- foreach (string d in dir)
- {
- if (d.Contains("PROB"))
- getdir(d, extName);//递归
- }
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- }
- }
|