Search Forum
(58019 Postings)
Search Site/Articles

Archived Articles
712 Articles

C# Books
C# Consultants
What Is C#?
Download Compiler
Code Archive
Archived Articles
Advertise
Contribute
C# Jobs
Beginners Tutorial
C# Contractors
C# Consulting
Links
C# Manual
Contact Us
Legal

GoDiagram for .NET from Northwoods Software www.nwoods.com


              
Printable Version

File Watcher Utility
By Prashant Brall

The FileWatcher utility is cool tool to see changes to a file(s) in a particular directory. Whether it is a log file creating by some batch processing program or a report file generated by a reporting engine. All you have to do is specify the path and the type of file(s) you are looking and this program will automatically fire events on creating / renaming / deletion of files.

Functionality wise it is something sort of Acorbat Distiller. :) Happy dot Netting !!!

-----------------------------------------------------

using System;
using System.IO;
/**************************************************\
* Program Name : File Watcher utility            *
* Author:Prashant Brall                          *
* E-mail: PrashantBrall@hotmail.com              *
* Dated:August 29,2002                           *
\*************************************************/

public class FileWatcher
{
   public FileWatcher()
   {
	string fpath = @"F:\Csharp";
	//you can specify a file type or a specific filename as
	//the second parameter of FileSystemWatcher or *.* for all
	//type of files
	FileSystemWatcher WatchFile = new FileSystemWatcher(fpath, "*.rpt");
	WatchFile.Created += new FileSystemEventHandler(this.FileCreated);
	WatchFile.Renamed += new RenamedEventHandler(this.FileReNamed);
	WatchFile.Deleted += new FileSystemEventHandler(this.FileDeleted);
	WatchFile.EnableRaisingEvents = true;
   }

   public void FileCreated(object sender, FileSystemEventArgs e)
   {
	Console.WriteLine(e.Name); //or anything you wish to display
	//do the processing of file and print it to
	//pdf writer port...or to a printer
   }
   public void FileReNamed(object sender, RenamedEventArgs e)
   {
	Console.WriteLine("\nFile Renamed:\n");

	Console.WriteLine("Change Type:   {0}", e.ChangeType );
	Console.WriteLine("Full Path:     {0}", e.FullPath   );
	Console.WriteLine("Name:          {0}", e.Name       );
	Console.WriteLine("Old Full Path: {0}", e.OldFullPath);
	Console.WriteLine("Old Name:      {0}", e.OldName    );
   }

   public void FileDeleted(object sender,FileSystemEventArgs e)
   {
	Console.WriteLine(e.ToString());
   }
   static void Main(string[] args)
   {
	FileWatcher fw = new FileWatcher();
	//sits idle and watches for any creation,renaming
	//or deletion of file best use of this program is
	//to run it as a windows service.
	Console.ReadLine();
   }
}