Alexander Shargin (rudankort@softspb.com), May 16, 2003.
I want my application to be notified about all changes in the file system. How can I do this?
On Pocket PC 2000 there was no good solution for this problem. But on Pocket PC 2002 Microsoft introduced a very convenient function which does just that - SHChangeNotifyRegister. This function is used to subscribe a window to receive notifications on file system changes. With this function you can watch a single directory (with subdirectories or without them) or the entire file system. You can subscribe for all available notifications or for just some of them (directory removal, storage card insertion etc.).
To get file system change notifications in your program, follow these 3 steps.
First, call SHChangeNotifyRegister to subscribe one of your windows to receive notifications. You can call it when the window is created (e. g. in WM_CREATE handler) or later when you need to begin getting notifications. First parameter of this function is HWND of your window. Second parameter is a pointer to SHCHANGENOTIFYENTRY structure. This structure specifies what directory you want to watch and what notifications you want to get. For instance, if you need to react on folder creation in \Windows directory (without subdirectories), you can use this code:
You can find description of all flags for SHCHANGENOTIFYENTRY::dwEventMask in docs. You can also pass NULL as the second parameter of SHChangeNotifyRegister to get all notifications for the entire file system.
Second, handle WM_FILECHANGEINFO message in the window proc of the window registered with SHChangeNotifyRegister. WPARAM of this message is not used but LPARAM holds pointer to FILECHANGENOTIFY structure with all information you need - event ID and info on file system object being changed. You can find detailed description of this structure in docs.
If you are using MFC, note that ClassWizard doesn't support WM_FILECHANGEINFO message, so you will need to add its handler manually.
Third, deregister your window when you no longer need to get file system notifications. Use SHChangeNotifyDeregister to do this:
|
|
FileNotificationsDemo project (18 Kb) illustrates all this steps. It intercepts all file system notifications and outputs their description in a text box. The screenshots shows this program's output after I created folder "\New Folder", then renamed it to "'My cool folder" and then deleted it from the device. |