文件监视。每过10秒钟检查一次。发现并返回新增文件的路径。10秒钟时间的检查是这样的一个算法//include <time.h>
double temp=time();
while (difftime(time(),temp)>10)
{
temp=time();
........//在这里实现是查找比较的算法。
...
存储当前文件夹下的文件。
与前一次记录的比较。
记录不同的元素。
返回结果的路径。
}
存储当前文件夹下的文件。:我有一个这样的设计。这个是一个这样的函数:
void CFileFindDlg::find() 
{
CFileFind FileFind;
BOOL bFlag;
bFlag = FileFind.FindFile("d:\\ccc\\*.jpg"); while(bFlag)
{
// 查找文件
bFlag=FileFind.FindNextFile(); //存储文件的名字
listBox.AddString(FileFind.GetFilePath());
}
CFileFind FileFind1;
BOOL bFlag1;
bFlag1 = FileFind1.FindFile("d:\\ccc\\*.txt");

while(bFlag1)
{
// 查找文件
bFlag1=FileFind1.FindNextFile();

//存储文件的名字
listBox.AddString(FileFind1.GetFilePath());
}
}指定的目录是:d:\\ccc
我现在想尽快实现这个设计。最后有一点地方还不是太容易。看看还有更好的算法吗?
有什么需要的再留言吧。我一直在。

解决方案 »

  1.   

    我在以前的csdn上找到一个例子。可是这个代码我感觉太复杂了。算法也不能理解,也不是时间间隔检查的那种算法。
    请多多赐教。谢谢  #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);   
      }   
      

  2.   

    这个过程并不如楼主说的那么复杂吧。
    用FindFirstChangeNotification函数监视文件变化情况,当出现文件变化(删除或更新等),事件对象将被置为有信号状态,根据参数判别更改类型,做出处理即可。
    楼主可看一下下面链接,网上人翻译好的:http://hi.baidu.com/sadusaga/blog/item/b7396639bcb51df53b87cec2.html
      

  3.   

    如果单单是每10秒钟检查一次,这个可以用Timer定时器,但是这个每次都要自己判断文件相关内容(新增,删除等,甚至目录操作),这个感觉不如你贴出的例子简单。在监视目录中出现所监视事件则接受系统通知,这个还是比较简单可行的。
      

  4.   


    不知道yyunffu是否自己有进行过验证?
    我之前用FindFirstChangeNotification来做文件监控,发现很多文件是没办法捕获到的…在CodeProject有一个采用ReadDirectoryChangesW 函数的做法,并封装成类了,使用很方便
    http://www.codeproject.com/KB/files/directorychangewatcher.aspx如果要做的更好的,可以考虑FileMon的代码
      

  5.   

    应该是如二楼所说的那样....有很多东西只是不了解...所以去做了很多额外的工作..其实后面才发现人家已经写好了API的
      

  6.   

    SHChangeNotifyRegister/SHChangeNotifyDeregister
    http://blog.csdn.net/codewarrior/archive/2004/06/15/12038.aspx