.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

FileSystemWatcher

Posted By: Asha.MP     Posted Date: September 21, 2010    Points:2   Category :ASP.Net
How to use this for text files?

Responses
Author: Deco             
Posted Date: September 21, 2010     Points: 5   


//NameSpaces
using System;
using System.IO;


public class FileWatcher
{
public FileWatcher()
{
string fpath = @"D:\MyFolder";
//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, "*.txt");
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();
}
}


Regards,
Deco


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend