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 = "*.*"; 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(); timer1.Interval = 600000; timer1.Start(); } private void Watcher_Created(object sender, FileSystemEventArgs e) { if (e.FullPath.Substring(e.FullPath.LastIndexOf(".") + 1) == "jdf" || e.FullPath.Substring(e.FullPath.LastIndexOf(".") + 1) == "njdf") { if (!e.FullPath.Contains("RECYCLE")) { string FullName = e.FullPath; 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"); DoLog(FullName); } else { richTextBox1.AppendText("不存在文件" + FullName + "\n"); } //不存在同名的文件则进行转换 if (!File.Exists(StartPath + Filename + ".xls") && File.Exists(FullName)) { if (FullName.Substring(FullName.LastIndexOf(".") + 1) == "jdf") { exec(@"D:\FileWatcher\JDF\DTS-JDFData2Excel.exe", @"D:\FileWatcher\JDF\DTS-JDFData2Excel.exe " + FullName); } else if (FullName.Substring(FullName.LastIndexOf(".") + 1) == "njdf") { exec(@"D:\FileWatcher\NJDF\DTS-Data2Excel.exe", @"D:\FileWatcher\NJDF\DTS-Data2Excel.exe " + FullName); } } } } } public void exec(string exePath, string parameters) { Process process = new Process(); process.StartInfo.FileName = exePath; process.StartInfo.Arguments = parameters; process.StartInfo.UseShellExecute = false; process.StartInfo.CreateNoWindow = true; process.StartInfo.RedirectStandardOutput = true; process.Start(); } private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e) { } public void DoLog(string Message) { try { StreamWriter sw = File.AppendText(DateTime.Now.ToString("yyyy-MM-dd") + ".txt"); sw.WriteLine("\n【时间】" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + Message + "\n"); sw.Close(); } catch (Exception) { } } /// /// 私有变量 /// private List lst = new List(); /// /// 获得目录下所有文件或指定文件类型文件(包含所有子文件夹) /// /// 文件夹路径 /// 扩展名可以多个 例如 .mp3.wma.rm /// List public List getFile(string path, string extName) { getdir(path, extName); return lst; } /// /// 私有方法,递归获取指定类型文件,包含子文件夹 /// /// /// 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; } } private void timer1_Tick(object sender, EventArgs e) { Process[] processes1 = Process.GetProcessesByName("DTS-JDFData2Excel"); for (int i = 0; i < processes1.Length; i++) { processes1[i].Kill(); } Process[] processes2 = Process.GetProcessesByName("DTS-Data2Excel"); for (int i = 0; i < processes2.Length; i++) { processes2[i].Kill(); } } } }