• >

Setting FileSystemWatcher

I have to watch for changes in INI file so needed somehow to put an event which should trigger an action in my program.FileSystemWatcher class is exactly what I needed.It’s easy to use and very helpful so I will paste the codesnippet that I have found:
/*
* C# Programmers Pocket Consultant
* Author: Gregory S. MacBeth
* Email: gmacbeth@comporium.net
* Create Date: June 27, 2003
* Last Modified Date:
* Version: 1
*/
using System;
using System.IO;

namespace Client.Chapter_11___File_and_Streams
{
public class TestChapter_11___File_and_Streams {
public static void Main(string[] args)
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = @”c:\Test”;
watcher.NotifyFilter =
NotifyFilters.LastAccess |
NotifyFilters.LastWrite |
NotifyFilters.FileName |
NotifyFilters.DirectoryName;
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;

}

public static void OnChanged(object source, FileSystemEventArgs e)
{
Console.WriteLine(“Event Fired”);
}
public static void OnRenamed(object source, RenamedEventArgs e)
{
Console.WriteLine(“Event Fired”);
}

}
}

Another good example from the CodeProject.

Be Sociable, Share!
This entry was posted in C#, Code, Programming and tagged , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • isisun photos

  • You have 0 posts in pdf

  • Categories

  • posidev.com

    Valid XHTML 1.0 Transitional

    Tracked by ClickAider

  • Charts

  • Reading