Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace FileSystemWatcherQ2_0560
{
    class Program
    {
        static void Main(string[] args)
        {
            FileSystemWatcher watcher = new FileSystemWatcher();
            Console.WriteLine("Please type the directory you would like to watch:");
            watcher.Path = Console.ReadLine();
            watcher.Filter = "*.txt";

            watcher.Changed += new FileSystemEventHandler(OnChanged);
            watcher.Created += new FileSystemEventHandler(OnChanged);
            watcher.Deleted += new FileSystemEventHandler(OnChanged);
            watcher.Renamed += new RenamedEventHandler(OnRenamed);
            watcher.EnableRaisingEvents = true;
            Console.WriteLine("Press 'Q' to quit");
            while (Console.Read() != 'q') ;
        }
        private static void OnChanged(object source, FileSystemEventArgs e)
        {
            Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
        }
        private static void OnRenamed(object source, RenamedEventArgs e)
        {
            Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
        }
        private static void OnDeleted(object source, RenamedEventArgs e)
        {
            Console.WriteLine("File: {0} deleted to {1}", e.OldFullPath, e.FullPath);
        }
    }
}
Posted
Comments
lewax00 22-Apr-13 17:20pm    
So...what is your question? All I see is a code dump. Please tell us what the program is doing, what you expect it to do, and any exceptions it may have thrown. Otherwise, we probably won't be able to help.
La Moth 22-Apr-13 17:24pm    
It is suppossed to implement file watching and keep track of all the changes made to a file
[no name] 22-Apr-13 17:28pm    
And it does. So what is the problem?
lewax00 22-Apr-13 17:58pm    
So what is the problem? Again, without knowing what the difference between what you expect it do do and what it is doing, we can't help.
[no name] 22-Apr-13 17:28pm    
All this is is a code dump without an explanation. My output when I run your code:
Please type the directory you would like to watch:
c:\temp
Press 'Q' to quit
File: c:\temp\New Text Document.txt Created
File: c:\temp\New Text Document.txt renamed to c:\temp\test.txt

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900