views:

2324

answers:

5

Use case: 3rd party application wants to programatically monitor a text file being generated by another program. Text file contains data you want to analyze as it's being updated.

I'm finding a lot of answers to this question wrapped around FileSystemWatcher but let's say you are writing an application for a Windows machine and can't guarantee .NET is installed.

Are there any libraries out there available for this, or am I just going to have to roll my own solution then?

Thanks.

+8  A: 

You can monitor a directory with FindFirstChangeNotification works on any windows.
It's efficent if you know where the file is - otherwise you can use the virtual driver/Filemon described below to check for changes anywhere on the system.

Example code here

Martin Beckett
+3  A: 

A simpler solution could be to check the last modified time stamp of the file.

If you use the _stat64() function to do this, it becomes a cross-platform solution.

Example code:

struct __stat64 fileinfo;

if(-1 != _stat64(filename, &fileinfo)
   return fileinfo.st_mtime;
Adam Pierce
It's worth noting that FindFirstChangeNotification will not work on single files.
Gearoid Murphy
+1  A: 

This sounds a lot like what FileMon, from sysinternals (now MS) does. They do this by creating a virtual device driver that is dynamically loaded. they have a good description of how it works here:

How FileMon Works

For the Windows 9x driver, the heart of FileMon is in the virtual device driver, Filevxd.vxd. It is dynamically loaded, and in its initialization it installs a file system filter via the VxD service, IFSMGR_InstallFileSystemApiHook, to insert itself onto the call chain of all file system requests. On Windows NT the heart of FileMon is a file system driver that creates and attaches filter device objects to target file system device objects so that FileMon will see all IRPs and FastIO requests directed at drives. When FileMon sees an open, create or close call, it updates an internal hash table that serves as the mapping between internal file handles and file path names. Whenever it sees calls that are handle based, it looks up the handle in the hash table to obtain the full name for display. If a handle-based access references a file opened before FileMon started, FileMon will fail to find the mapping in its hash table and will simply present the handle's value instead.

Joe Basirico
A: 

Is there any solution for MFC to monitor changes within a folder. thanks.

chris