Monday, November 15, 2010

File system watcher syncs to a private folder

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace FileUploader
{
    class Program
    {
        static void Main(string[] args)
        {
            Run();
           

        }

        public static void Run()
        {

            FileSystemWatcher Watcher = new FileSystemWatcher(Environment.GetEnvironmentVariable("USERPROFILE"));
            Watcher.Path = @"E:\Sameer";
            Watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
            Watcher.Filter = "*.txt";
            Watcher.Created += new FileSystemEventHandler(Watcher_Created);
            Watcher.Changed += new FileSystemEventHandler(Watcher_Changed);
            Watcher.Renamed += new RenamedEventHandler(Watcher_Renamed);
            Watcher.Deleted += new FileSystemEventHandler(Watcher_Deleted);

            Watcher.EnableRaisingEvents = true;
            Console.ReadKey();
        }
        static void Watcher_Created(object sender, FileSystemEventArgs e)
        {
            try
            {
                string fileName = System.IO.Path.GetFileName(e.FullPath);

                Console.WriteLine("File : " + e.FullPath + " Change Type is : " + e.ChangeType);

                System.IO.File.Copy(e.FullPath, @"C:\Users\sameer\Documents\Visual Studio 2008\Projects\FileUploader\FileUploader\Uploaded Docs\" + fileName , true);
               
            }
            catch(Exception ex)
            {
                Console.WriteLine("Problem occured");
            }
           
        }

        static void Watcher_Deleted(object sender, FileSystemEventArgs e)
        {
            Console.WriteLine("File : " + e.FullPath + " Change Type is : " + e.ChangeType);
        }

        static void Watcher_Renamed(object sender, RenamedEventArgs e)
        {
            try
            {
                Console.WriteLine("File : " + e.FullPath + " Change Type is : " + e.ChangeType);
                string fileName = System.IO.Path.GetFileName(e.FullPath);
                System.IO.File.Move(@"C:\Users\sameer\Documents\Visual Studio 2008\Projects\FileUploader\FileUploader\Uploaded Docs\" + e.OldName, @"C:\Users\sameer\Documents\Visual Studio 2008\Projects\FileUploader\FileUploader\Uploaded Docs\" + fileName);
            }

            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        static void Watcher_Changed(object sender, FileSystemEventArgs e)
        {
            Console.WriteLine("File : " + e.FullPath + " Change Type is : " + e.ChangeType);
            try
            {
                string fileName = System.IO.Path.GetFileName(e.FullPath);

                Console.WriteLine("File : " + e.FullPath + " Change Type is : " + e.ChangeType);

                System.IO.File.Copy(e.FullPath, @"C:\Users\sameer\Documents\Visual Studio 2008\Projects\FileUploader\FileUploader\Uploaded Docs\" + fileName ,true );
            }
            catch (Exception ex)
            {
                Console.WriteLine("Problem occured" + ex.ToString());
            }
        }


    }
}

No comments:

Post a Comment