Click here to Skip to main content
15,896,726 members
Articles / Programming Languages / C#

File Flitter

Rate me:
Please Sign up or sign in to vote.
4.94/5 (23 votes)
15 Jan 2011CPOL10 min read 51.5K   799   68  
Monitor files and when they change, copy them to specified folders.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;

namespace FileFlit
{
	public class Monitor
	{
		protected List<FileSystemWatcher> watchList;
		protected static Monitor monitor;

		public static void Go(List<FlitterRecord> fileList)
		{
			if (monitor == null)
			{
				monitor = new Monitor();
			}

			monitor.WatchFiles(fileList);
		}

		public static void UpdateNow(List<FlitterRecord> fileList)
		{
			foreach (FlitterRecord fr in fileList)
			{
				try
				{
					File.Copy(fr.SourceFile, Path.Combine(fr.DestinationFolder, Path.GetFileName(fr.SourceFile)), true);
					Form1.Log(true, fr);
				}
				catch (Exception e)
				{
					Form1.Log(false, fr, e.Message);
				}
			}
		}

		public Monitor()
		{
			watchList = new List<FileSystemWatcher>();
		}

		protected void WatchFiles(List<FlitterRecord> fileList)
		{
			foreach (FileSystemWatcher fsw in watchList)
			{
				fsw.Changed -= OnChanged;
			}

			List<FileSystemWatcher> newWatchList = new List<FileSystemWatcher>();

			foreach (FlitterRecord fr in fileList)
			{
				FlitterFileSystemWatcher fsw = new FlitterFileSystemWatcher(Path.GetDirectoryName(fr.SourceFile), Path.GetFileName(fr.SourceFile)) { FlitterRecord = fr };
				fsw.NotifyFilter = NotifyFilters.LastWrite;
				fsw.Changed += new FileSystemEventHandler(OnChanged);
				fsw.EnableRaisingEvents = true;
				newWatchList.Add(fsw);
			}

			watchList = newWatchList;
		}

		protected void OnChanged(object sender, FileSystemEventArgs e)
		{
			FlitterRecord fr = ((FlitterFileSystemWatcher)sender).FlitterRecord;
			bool success=false;
			int retries = 3;

			while (!success)
			{
				try
				{
					Thread.Sleep(fr.Delay);
					File.Copy(fr.SourceFile, Path.Combine(fr.DestinationFolder, Path.GetFileName(fr.SourceFile)), true);
					success = true;
				}
				catch(Exception ex)
				{
					if (--retries == 0)
					{
						Form1.Log(false, fr, ex.Message);
						break;
					}
				}

				if (success)
				{
					Form1.Log(true, fr);
				}
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Interacx
United States United States
Blog: https://marcclifton.wordpress.com/
Home Page: http://www.marcclifton.com
Research: http://www.higherorderprogramming.com/
GitHub: https://github.com/cliftonm

All my life I have been passionate about architecture / software design, as this is the cornerstone to a maintainable and extensible application. As such, I have enjoyed exploring some crazy ideas and discovering that they are not so crazy after all. I also love writing about my ideas and seeing the community response. As a consultant, I've enjoyed working in a wide range of industries such as aerospace, boatyard management, remote sensing, emergency services / data management, and casino operations. I've done a variety of pro-bono work non-profit organizations related to nature conservancy, drug recovery and women's health.

Comments and Discussions