views:

76

answers:

2

Is it possible to break at runtime when a particular file has been modified?

ie. monitor the file and break into a debugger once a change has been made to it.

This is for a windows app...is this possible in visual studio or windbg?

edit: i should have mentioned that this is for a Win32 app..

A: 

Assuming this is .NET, the System.IO.FileSystemWatcher class is what you need.

FileSystemWatcher watcher = new FileSystemWatcher("c:filename.txt");
watcher.Changed += new FileSystemEventHandler(watcher_Changed);
// 
void watcher_Changed(object sender, FileSystemEventArgs e)
{
    // put a breakpoint here
}
MusiGenesis
i should have mentioned that this is for a Win32 app..thanks for the answer, its good to know!
e k
+2  A: 

you can use the System.IO.FileSystemWatcher class.

FileSystemWatcher watcher = = new FileSystemWatcher();
watcher.Filter = @"myFile.ini";
watcher.Changed += new FileSystemEventHandler(watcher_Changed);

and then you implement the delegate of type FileSystemEventHandler:

static void watcher_Changed(object sender, FileSystemArgs e)
{
    Console.WriteLine("File {0} has changed.", e.FullPath );
}

every time the file you have selected in the filter is modified, you get an alert (you can use both a Debug class or Trace class to output data). Moreover the FileSystemWatcher class has more events (Renamed, Deleted, Created).

Enreeco
I think you mean FileSystemEventArgs instead of FileSystemArgs (unless this is different in .NET 3.5 or something)
MusiGenesis