Watching Folder With C# FileSystemWatcher and Beeping When A Change Happen
The title explains what this code does, Google is the reason that i write long titles :)
This is a console application which listens a folder. If a change occurs in that folder you get an alert with a message box. Also the console screen lists the files in folder.
This type of work is better with windows services. Because if you close the console application you can not get an alert :)
using System; using System.IO; using System.Runtime.InteropServices; using System.Windows.Forms; namespace FolderAlertService { class Program { //to beep [DllImport("Kernel32.dll")] public static extern bool Beep(UInt32 frequency, UInt32 duration); static void Main(string[] args) { FileSystemWatcher fsw = new FileSystemWatcher(@"c:\Serdar"); fsw.EnableRaisingEvents = true; fsw.Changed += new FileSystemEventHandler(OnChanged); Console.Read(); } private static void OnChanged(object source, FileSystemEventArgs e) { Console.Clear(); Beep(500, 100); MessageBox.Show("Folder Changed!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification); string[] files = Directory.GetFiles(e.FullPath); for (int i = 0; i < files.Length; i++) { Console.WriteLine(string.Format("File : {0}", Path.GetFileName(files[i]))); } } } }
Category: Csharp - C# - 2 comments »






May 13th, 2010 at 2:29 pm
Nice post! I got “FolderAlertService.vshost.exe” showing up on task manager, but I couldn’t get a beep or a message box to pop up or the console to feed back anything when I changed something inside of the folder I specified. My code is an exact copy of your code except for this line:
>> FileSystemWatcher fsw = new FileSystemWatcher(@”c:/listen”);
Everything compiles fine. Any idea what could be preventing the Event from triggering? Thanks!
May 18th, 2010 at 10:41 pm
i am not sure, but maybe your path is wrong…