(转)FindFirstChangeNotification,创建一个文件通知对象,该对象用于监视文件系统发生的变化

FindFirstChangeNotification

VB声明
Declare Function FindFirstChangeNotification Lib "kernel32" Alias "FindFirstChangeNotificationA" (ByVal lpPathName As String, ByVal bWatchSubtree As Long, ByVal dwNotifyFilter As Long) As Long
说明
创建一个文件通知对象。该对象用于监视文件系统发生的变化
返回值
Long,如成功,返回一个改变通知对象的句柄;INVALID_HANDLE_VALUE表示失败。会设置GetLastError
参数表
参数类型及说明
lpPathNameString,要监视的目录
bWatchSubtreeLong,如果为TRUE,表示监视lpPathName的所有子目录
dwNotifyFilterLong,带有前缀FILE_NOTIFY_CHANGE_???前缀的一个或多个常数,它们指定了对象发出信号的条件
注解

FindCloseChangeNotification函数关闭句柄,不要用CloseHandle函数

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <tchar.h>

void RefreshDirectory(LPTSTR);
void RefreshTree(LPTSTR);

void WatchDirectory(LPTSTR lpDir)
{
   DWORD dwWaitStatus; 
   HANDLE dwChangeHandles[2]; 
   TCHAR lpDrive[4];
   TCHAR lpFile[_MAX_FNAME];
   TCHAR lpExt[_MAX_EXT];

   _tsplitpath(lpDir, lpDrive, NULL, lpFile, lpExt);

   lpDrive[2] = (TCHAR)'//';
   lpDrive[3] = (TCHAR)'/0';
 
// Watch the directory for file creation and deletion. 
 
   dwChangeHandles[0] = FindFirstChangeNotification( 
      lpDir,                         // directory to watch 
      FALSE,                         // do not watch subtree 
      FILE_NOTIFY_CHANGE_FILE_NAME); // watch file name changes 
 
   if (dwChangeHandles[0] == INVALID_HANDLE_VALUE) 
      ExitProcess(GetLastError()); 
 
// Watch the subtree for directory creation and deletion. 
 
   dwChangeHandles[1] = FindFirstChangeNotification( 
      lpDrive,                       // directory to watch 
      TRUE,                          // watch the subtree 
      FILE_NOTIFY_CHANGE_DIR_NAME);  // watch dir. name changes 
 
   if (dwChangeHandles[1] == INVALID_HANDLE_VALUE) 
      ExitProcess(GetLastError()); 
 
// Change notification is set. Now wait on both notification 
// handles and refresh accordingly. 
 
   while (TRUE) 
   { 
   // Wait for notification.
 
      dwWaitStatus = WaitForMultipleObjects(2, dwChangeHandles, 
         FALSE, INFINITE); 
 
      switch (dwWaitStatus) 
      { 
         case WAIT_OBJECT_0: 
 
         // A file was created or deleted in the directory.
         // Refresh this directory and restart the notification.
 
            RefreshDirectory(lpDir); 
            if ( FindNextChangeNotification( 
                    dwChangeHandles[0]) == FALSE ) 
                ExitProcess(GetLastError()); 
            break; 
 
         case WAIT_OBJECT_0 + 1: 
 
         // A directory was created or deleted in the subtree.
         // Refresh the tree and restart the notification.
 
            RefreshTree(lpDrive); 
            if (FindNextChangeNotification( 
                    dwChangeHandles[1]) == FALSE) 
                ExitProcess(GetLastError()); 
            break; 
 
        default: 
            ExitProcess(GetLastError()); 
      }
   }
}

void RefreshDirectory(LPTSTR lpDir)
{
   _tprintf(TEXT("Refresh the directory (%s)./n"), lpDir);
}

void RefreshTree(LPTSTR lpDrive)
{
   _tprintf(TEXT("Refresh the directory tree (%s)./n"), lpDrive);
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
 
from msdn: Monitoring Changes in a Directory or Directory Tree The following example monitors the directory tree starting at C:/ for directory name changes. It also monitors the C:/WINDOWS directory for file name changes.  The example uses the FindFirstChangeNotification function to create two notification handles and the WaitForMultipleObjects function to wait on the handles. Whenever a directory is created or deleted in the tree starting at C:/ , the example updates the entire directory tree. Whenever a file is created or deleted in the C:/WINDOWS directory, the example refreshes the WINDOWS directory. The FindNextChangeNotification function restarts the change notification each time the example processes a change.  DWORD dwWaitStatus;  HANDLE dwChangeHandles[2];    // Watch the C:/WINDOWS directory for file creation and  // deletion.    dwChangeHandles[0] = FindFirstChangeNotification(      "C://WINDOWS",                 // directory to watch      FALSE,                         // do not watch the subtree      FILE_NOTIFY_CHANGE_FILE_NAME); // watch file name changes    if (dwChangeHandles[0] == INVALID_HANDLE_VALUE)      ExitProcess(GetLastError());    // Watch the C:/ subtree for directory creation and  // deletion.    dwChangeHandles[1] = FindFirstChangeNotification(      "C://",                        // directory to watch      TRUE,                          // watch the subtree      FILE_NOTIFY_CHANGE_DIR_NAME);  // watch dir. name changes    if (dwChangeHandles[1] == INVALID_HANDLE_VALUE)      ExitProcess(GetLastError());    // Change notification is set. Now wait on both notification  // handles and refresh accordingly.    while (TRUE)  {        // Wait for notification.       dwWaitStatus = WaitForMultipleObjects(2, dwChangeHandles,          FALSE, INFINITE);        switch (dwWaitStatus)      {          case WAIT_OBJECT_0:            // A file was created or deleted in C:/WINDOWS.          // Refresh this directory and restart the          // change notification. RefreshDirectory is an          // application-defined function.                RefreshDirectory("C://WINDOWS")              if ( FindNextChangeNotification(                      dwChangeHandles[0]) == FALSE )                  ExitProcess(GetLastError());              break;            case WAIT_OBJECT_0 + 1:            // A directory was created or deleted in C:/.          // Refresh the directory tree and restart the          // change notification. RefreshTree is an          // application-defined function.                RefreshTree("C://");              if (FindNextChangeNotification(                      dwChangeHandles[1]) == FALSE)                  ExitProcess(GetLastError());              break;            default:              ExitProcess(GetLastError());      }  }

转自:http://blog.csdn.net/zhaoyawei/article/details/742276

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值