News | Articles | Libraries | Developer Tools | Books | Forum Links | Search   
Sections:
 

QA: How to get file system change notification?

By Alexander Shargin, May 16, 2003.
Print version

Question

I want my application to be notified about all changes in the file system. How can I do this?

Answer

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.

Step 1

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:

SHCHANGENOTIFYENTRY cne; cne.dwEventMask = SHCNE_MKDIR; cne.pszWatchDir = _T("\\Windows"); cne.fRecursive = FALSE; SHChangeNotifyRegister(hWnd, &cne);

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.

Step 2

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.

Step 3

Third, deregister your window when you no longer need to get file system notifications. Use SHChangeNotifyDeregister to do this:

SHChangeNotifyDeregister(hWnd);

Sample

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.

Related resources:

Discuss

Discuss this article. Here you can write your comments and read comments of other developers.
Rate this article:     Poor Excellent    
 12345 
© 2001-2005 Pocket PC Developer Network, a division of Spb Software House