C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » File Operations »

Watch your folder


Posted Date: 20 Aug 2009    Resource Type: Code Snippets    Category: File Operations
Author: Shunmuganathan MMember Level: Diamond    
Rating: 1 out of 5Points: 7



Description :


We can watch particular folder.
If any files created,accessed,changed or deleted we can get that information.
This code will very useful for log process.

Name spaces

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;


Code Segment


namespace ConsoleApplication1
{
public class Program
{
static void Main(string[] args)
{
FileSystemWatcher MyFileSystemWatcher = new FileSystemWatcher();
try
{
MyFileSystemWatcher.Path = @"D:\Nathan";
}
catch (ArgumentException ex)
{
Console.WriteLine(ex.Message);
return;
}

MyFileSystemWatcher.NotifyFilter = NotifyFilters.LastAccess
| NotifyFilters.LastWrite
| NotifyFilters.FileName
| NotifyFilters.DirectoryName;

MyFileSystemWatcher.Filter = "*.txt";

MyFileSystemWatcher.Changed += new FileSystemEventHandler(OnChanged);
MyFileSystemWatcher.Created += new FileSystemEventHandler(OnChanged);
MyFileSystemWatcher.Deleted += new FileSystemEventHandler(OnChanged);
MyFileSystemWatcher.Renamed += new RenamedEventHandler(OnRenamed);
MyFileSystemWatcher.EnableRaisingEvents = true;

Console.WriteLine(@"Press 'q' to quit app.");
while (Console.Read() != 'q') ;
}
private static void OnChanged(object source, FileSystemEventArgs e)
{
Console.WriteLine("File: {0} {1}!", e.FullPath, e.ChangeType);
}

private static void OnRenamed(object source, RenamedEventArgs e)
{
Console.WriteLine("File: {0} renamed to\n{1}", e.OldFullPath, e.FullPath);
}
}
}



1. Create instance of MyFileSystemWatcher
2. Add the NotifyFilter
3. Add the events
4. Run the application

by
Nathan



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Watch your folder  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Directory Tree view
Previous Resource: File search without recursion
Return to Discussion Resource Index
Post New Resource
Category: File Operations


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use