Home All Groups Group Topic Archive Search About

FileSystemWatcher in a Class Lib

Author
20 Jun 2006 1:55 PM
Cliff
I'm having a problem getting events out of the FileSystemWatcher Class

I can get the following Code to work if its on a Form, however I'm
writing a class library and when this code exists in the Class Lib DLL
and is called from a form, the events don't fire.

Anyone got any idea whats going wrong?

            FileSystemWatcher objFSW = new FileSystemWatcher();
            objFSW.Path = "c:\\";
            objFSW.IncludeSubdirectories = true;
            objFSW.Filter = "*.*";
            objFSW.NotifyFilter = NotifyFilters.LastAccess |
                                    NotifyFilters.LastWrite |
                                NotifyFilters.FileName |
                                    NotifyFilters.DirectoryName;

            //Setup Events
            objFSW.Changed += new FileSystemEventHandler(FileChanged);
            objFSW.Created += new FileSystemEventHandler(FileCreated);
            objFSW.Deleted += new FileSystemEventHandler(FileDeleted);
            objFSW.Renamed += new RenamedEventHandler(FileRenamed);

            //Start Things up
            objFSW.EnableRaisingEvents = true;
        }

        public static void FileChanged(object source,
FileSystemEventArgs e)
        {
            Debug.WriteLine("File {0} was Changed.", e.FullPath);
        }

        public static void FileCreated(object source,
FileSystemEventArgs e)
        {
            Debug.WriteLine("File {0} was Created.", e.FullPath);
        }

        public static void FileDeleted(object source,
FileSystemEventArgs e)
        {
            Debug.WriteLine("File {0} was Deleted.", e.FullPath);
        }

        public static void FileRenamed(object source, RenamedEventArgs
e)
        {
            Debug.WriteLine("File " + e.OldFullPath + " was renamed to
" +  e.FullPath);
        }

Thanks in advance

Cliff.

AddThis Social Bookmark Button