Filewatcher

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

namespace FileSystemWatcherLab
{
    internal class Test1
    {
        public void Execute()
        {
            FileSystemWatcher watcher = new FileSystemWatcher();
            watcher.Path = "S:\\Filewatcher";
            watcher.NotifyFilter = NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastWrite | NotifyFilters.Size;
            watcher.IncludeSubdirectories = true;

            watcher.Changed += this.OnChanged;
            watcher.Created += this.OnCreated;
            watcher.Deleted += this.OnDeleted;
            watcher.Renamed += this.OnRenamed;

            watcher.EnableRaisingEvents = true;
        }

        /// <summary>
        /// On path changed event.
        /// </summary>
        /// <param name="source">Changed source.</param>
        /// <param name="e">Path change event args.</param>
        private void OnCreated(object source, FileSystemEventArgs e)
        {
            Console.WriteLine("Created");
            Console.WriteLine(e.FullPath);
            Console.WriteLine();
        }

        /// <summary>
        /// On path changed event.
        /// </summary>
        /// <param name="source">Changed source.</param>
        /// <param name="e">Path change event args.</param>
        private void OnDeleted(object source, FileSystemEventArgs e)
        {
            Console.WriteLine("Deleted");
            Console.WriteLine(e.FullPath);
            Console.WriteLine();
        }

        /// <summary>
        /// On path changed event.
        /// </summary>
        /// <param name="source">Changed source.</param>
        /// <param name="e">Path change event args.</param>
        private void OnChanged(object source, FileSystemEventArgs e)
        {
            Console.WriteLine("Changed");
            Console.WriteLine(e.FullPath);
            Console.WriteLine();
        }

        /// <summary>
        /// On path rename event.
        /// </summary>
        /// <param name="source">Rename source.</param>
        /// <param name="e">Path rename args.</param>
        private void OnRenamed(object source, RenamedEventArgs e)
        {
            Console.WriteLine("Renamed");
            Console.WriteLine(e.FullPath);
            Console.WriteLine();
        }
    }
}
View Code

 

posted @ 2013-10-29 17:57  1280  阅读(287)  评论(0编辑  收藏  举报
不想当将军的厨师不是好裁缝
友荐云推荐
QQ:63219702