void DisplayFileSystemWatcherInfo(System.IO.WatcherChangeTypes watcherChangeTypes, string name, string oldName = null) { if (watcherChangeTypes == System.IO.WatcherChangeTypes.Created) { //Console.WriteLine(System.IO.Path.GetExtension(name)); //foreach(var i in filters) //{ // Console.WriteLine(i); //} string ext = System.IO.Path.GetExtension(name).Trim(new char[] { ' ', '.', '\t', '\r', '\n' }); if (!filters.Contains(ext) && !filters.Contains("." + ext)) { return; } Thread th = new Thread(ProcessItem); th.IsBackground = true; th.Start(new ResultInfo(name, watcherChangeTypes)); } //if (watcherChangeTypes == System.IO.WatcherChangeTypes.Renamed) //{ // // When using FileSystemWatcher event's be aware that these events will be called on a separate thread automatically!!! // // If you call method AddListLine() in a normal way, it will throw following exception: // // "The calling thread cannot access this object because a different thread owns it" // // To fix this, you must call this method using Dispatcher.BeginInvoke(...)! // Dispatcher.BeginInvoke(new Action(() => { AddListLine(string.Format("{0} -> {1} to {2} - {3}", watcherChangeTypes.ToString(), oldName, name, DateTime.Now)); })); //} //else //{ // Dispatcher.BeginInvoke(new Action(() => { AddListLine(string.Format("{0} -> {1} - {2}", watcherChangeTypes.ToString(), name, DateTime.Now)); })); //} }
private void OnCopy(System.IO.WatcherChangeTypes changeType, string subPath, string fileName) { if (this.Copy != null) { this.Copy(changeType, subPath, fileName); } }
// ---------------------------------------------------------------------------------- private void DisplayFileSystemWatcherInfo(System.IO.WatcherChangeTypes watcherChangeTypes, string name, string oldName = null) { if (this.OnFileChange != null && !HasAnotherFileEventOccuredRecently(name)) { OnFileChange(this, name); } }
private static void HandleFileCreated(string path, string fileName, System.IO.WatcherChangeTypes changeType) { if (changeType == WatcherChangeTypes.Created) { Thread.Sleep(3000); try { if (!File.Exists(path)) { // This statement ensures that the file is created, // but the handle is not kept. using (FileStream fs = File.Create(path)) { } } string[] args = Environment.GetCommandLineArgs(); string baseFolder = args[1]; string year = DateTime.Now.Year.ToString(); string[] monthNames = { "Janurary", "Feburary", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "September", "October", "November", "December" }; string monthName = monthNames[DateTime.Now.Month - 1]; string dayOfMonth = DateTime.Now.Day.ToString(); string datePath = System.IO.Path.Combine(baseFolder, year, monthName, dayOfMonth); if (!Directory.Exists(datePath)) { System.IO.Directory.CreateDirectory(datePath); } string destPath = System.IO.Path.Combine(datePath, fileName); // Ensure that the target does not exist. if (File.Exists(destPath)) { File.Delete(destPath); } // Move the file. Thread.Sleep(2000); File.Move(path, destPath, true); Console.WriteLine("{0} was moved to {1}.", path, destPath); // See if the original exists now. if (File.Exists(path)) { Console.WriteLine("The original file still exists, which is unexpected."); } else { Console.WriteLine("The original file no longer exists, which is expected."); } } catch (Exception e) { Console.WriteLine("The process failed: {0}", e.ToString()); } } ; }
private string GetChangeTypeMessage(WatcherChangeTypes changeType) { switch (changeType) { case WatcherChangeTypes.Created: return(WatcherChangeTypeMessages.Created); case WatcherChangeTypes.Renamed: return(WatcherChangeTypeMessages.Renamed); default: return(WatcherChangeTypeMessages.NotSupported); } }
// ---------------------------------------------------------------------------------- void DisplayFileSystemWatcherInfo(System.IO.WatcherChangeTypes watcherChangeTypes, string name, string oldName = null) { if (watcherChangeTypes == System.IO.WatcherChangeTypes.Renamed) { // When using FileSystemWatcher event's be aware that these events will be called on a separate thread automatically!!! // If you call method AddListLine() in a normal way, it will throw following exception: // "The calling thread cannot access this object because a different thread owns it" // To fix this, you must call this method using Dispatcher.BeginInvoke(...)! Dispatcher.BeginInvoke(new Action(() => { AddListLine(string.Format("{0} -> {1} to {2} - {3}", watcherChangeTypes.ToString(), oldName, name, DateTime.Now)); })); } else { Dispatcher.BeginInvoke(new Action(() => { AddListLine(string.Format("{0} -> {1} - {2}", watcherChangeTypes.ToString(), name, DateTime.Now)); })); } }