C++学习之:文件监视

 对于某个系统某个目录变化如何进行监视。监视包含文件的添加、修改、重命名和删除等。在VC驿站上看到的,现在写成blog发表于大家分享。

  
  
  
  
  1. //监视线程 
  2. DWORD WINAPI C***Dlg::ThreadWatchProc( LPVOID lParam ) 
  3.     C***Dlg *obj = (C***Dlg*)lParam;   
  4.  
  5.     obj->m_hDirectory = CreateFile(            //打开目录,得到目录的句柄 
  6.         obj->m_strWatchedDir, 
  7.         GENERIC_READ|GENERIC_WRITE, 
  8.         FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, 
  9.         NULL, 
  10.         OPEN_EXISTING, 
  11.         FILE_FLAG_BACKUP_SEMANTICS, 
  12.         NULL 
  13.         );  
  14.     if(obj->m_hDirectory ==INVALID_HANDLE_VALUE)  
  15.         return false
  16.  
  17.     char buf[(sizeof(FILE_NOTIFY_INFORMATION)+MAX_PATH)*2]; 
  18.     FILE_NOTIFY_INFORMATION* pNotify=(FILE_NOTIFY_INFORMATION*)buf; 
  19.     DWORD dwBytesReturned;         
  20.     while(true
  21.     { 
  22.         if(::ReadDirectoryChangesW(obj->m_hDirectory, 
  23.             pNotify, 
  24.             sizeof(buf), 
  25.             true
  26.             FILE_NOTIFY_CHANGE_FILE_NAME| 
  27.             FILE_NOTIFY_CHANGE_DIR_NAME| 
  28.             FILE_NOTIFY_CHANGE_ATTRIBUTES| 
  29.             FILE_NOTIFY_CHANGE_SIZE| 
  30.             FILE_NOTIFY_CHANGE_LAST_WRITE| 
  31.             FILE_NOTIFY_CHANGE_LAST_ACCESS| 
  32.             FILE_NOTIFY_CHANGE_CREATION| 
  33.             FILE_NOTIFY_CHANGE_SECURITY, 
  34.             &dwBytesReturned, 
  35.             NULL, 
  36.             NULL)) 
  37.         { 
  38.             char tmp[MAX_PATH], str1[MAX_PATH], str2[MAX_PATH]; 
  39.             memset(tmp, 0, sizeof(tmp));  
  40.             memset(str1,0, sizeof(str1)); 
  41.             memset(str2,0, sizeof(str2)); 
  42.             WideCharToMultiByte(CP_ACP,0,pNotify->FileName,pNotify->FileNameLength/2,tmp,99,NULL,NULL); 
  43.             strcpy(str1, tmp); 
  44.  
  45.             if(pNotify->NextEntryOffset != 0) 
  46.             { 
  47.                 PFILE_NOTIFY_INFORMATION p = (PFILE_NOTIFY_INFORMATION)((char*)pNotify+pNotify->NextEntryOffset); 
  48.                 memset(tmp, 0, sizeof(tmp)); 
  49.                 WideCharToMultiByte(CP_ACP,0,p->FileName,p->FileNameLength/2,tmp,99,NULL,NULL); 
  50.                 strcpy(str2, tmp); 
  51.             } 
  52.             switch(pNotify->Action) 
  53.             {                
  54.             case FILE_ACTION_ADDED: 
  55.                 if(obj->m_bAddNew) 
  56.                 { 
  57.                     CTime tt = CTime::GetCurrentTime();  
  58.                     CString strTT; 
  59.                     strTT.Format(_T("%d:%d:%d"),tt.GetHour(),tt.GetMinute(),tt.GetSecond()); 
  60.                     obj->m_listAllInfo.InsertItem(0,obj->m_pszListIndex); 
  61.                     obj->m_listAllInfo.SetItemText(0,2,_T("添加了新文件")); 
  62.                     obj->m_listAllInfo.SetItemText(0,3,str1); 
  63.                     obj->m_listAllInfo.SetItemText(0,1,strTT); 
  64.                      
  65.                     pCheckInfo PI = NULL; 
  66.                     PI = new tagCheckInfo(); 
  67.                     strcpy(PI->szIndex,obj->m_pszListIndex); 
  68.                     strcpy(PI->szTime,strTT); 
  69.                     strcpy(PI->szType,_T("添加了新文件")); 
  70.                     strcpy(PI->szDetail,str1); 
  71.                     obj->m_vecCI.push_back(PI); 
  72.                 } 
  73.                 break
  74.  
  75.             case FILE_ACTION_REMOVED: 
  76.                 if(obj->m_bDelete) 
  77.                 { 
  78.                     CTime tt=CTime::GetCurrentTime();    
  79.                     CString strTT; 
  80.                     strTT.Format(_T("%d:%d:%d"),tt.GetHour(),tt.GetMinute(),tt.GetSecond()); 
  81.                     obj->m_listAllInfo.InsertItem(0,obj->m_pszListIndex); 
  82.                     obj->m_listAllInfo.SetItemText(0,2,_T("删除了文件")); 
  83.                     obj->m_listAllInfo.SetItemText(0,3,str1); 
  84.                     obj->m_listAllInfo.SetItemText(0,1,strTT); 
  85.                      
  86.                     pCheckInfo PI = NULL; 
  87.                     PI = new tagCheckInfo(); 
  88.                     strcpy(PI->szIndex,obj->m_pszListIndex); 
  89.                     strcpy(PI->szTime,strTT); 
  90.                     strcpy(PI->szType,_T("删除了文件")); 
  91.                     strcpy(PI->szDetail,str1); 
  92.                     obj->m_vecCI.push_back(PI); 
  93.                 } 
  94.                 break
  95.  
  96.             case FILE_ACTION_RENAMED_NEW_NAME: 
  97.                 if(obj->m_bRename) 
  98.                 { 
  99.                     CTime tt = CTime::GetCurrentTime();  
  100.                     CString strTT; 
  101.                     strTT.Format(_T("%d:%d:%d"),tt.GetHour(),tt.GetMinute(),tt.GetSecond()); 
  102.                     obj->m_listAllInfo.InsertItem(0,obj->m_pszListIndex); 
  103.                     obj->m_listAllInfo.SetItemText(0,2,_T("重命名了文件")); 
  104.                     strcat(str1,_T("->")); 
  105.                     obj->m_listAllInfo.SetItemText(0,3,strcat(str1,str2)); 
  106.                     obj->m_listAllInfo.SetItemText(0,1,strTT); 
  107.                      
  108.                     pCheckInfo PI = NULL; 
  109.                     PI = new tagCheckInfo(); 
  110.                     strcpy(PI->szIndex,obj->m_pszListIndex); 
  111.                     strcpy(PI->szTime,strTT); 
  112.                     strcpy(PI->szType,_T("重命名了文件")); 
  113.                     strcpy(PI->szDetail,strcat(str1,str2)); 
  114.                     obj->m_vecCI.push_back(PI); 
  115.                 } 
  116.                 break
  117.  
  118.             case FILE_ACTION_RENAMED_OLD_NAME: 
  119.                 if(obj->m_bRename) 
  120.                 { 
  121.                     CTime tt=CTime::GetCurrentTime();    
  122.                     CString strTT; 
  123.                     strTT.Format(_T("%d:%d:%d"),tt.GetHour(),tt.GetMinute(),tt.GetSecond()); 
  124.                     obj->m_listAllInfo.InsertItem(0,obj->m_pszListIndex); 
  125.                     obj->m_listAllInfo.SetItemText(0,2,_T("重命名了文件")); 
  126.                     strcat(str1,_T(" 改名为 ")); 
  127.                     obj->m_listAllInfo.SetItemText(0,3,strcat(str1,str2)); 
  128.                     obj->m_listAllInfo.SetItemText(0,1,strTT); 
  129.                      
  130.                     pCheckInfo PI = NULL; 
  131.                     PI = new tagCheckInfo(); 
  132.                     strcpy(PI->szIndex,obj->m_pszListIndex); 
  133.                     strcpy(PI->szTime,strTT); 
  134.                     strcpy(PI->szType,_T("重命名了文件")); 
  135.                     strcpy(PI->szDetail,strcat(str1,str2)); 
  136.                     obj->m_vecCI.push_back(PI); 
  137.                 } 
  138.                 break
  139.  
  140.             case FILE_ACTION_MODIFIED: 
  141.                 if(obj->m_bModify) 
  142.                 { 
  143.                     CTime tt = CTime::GetCurrentTime();  
  144.                     CString strTT; 
  145.                     strTT.Format(_T("%d:%d:%d"),tt.GetHour(),tt.GetMinute(),tt.GetSecond()); 
  146.                     obj->m_listAllInfo.InsertItem(0,obj->m_pszListIndex); 
  147.                     obj->m_listAllInfo.SetItemText(0,2,_T("修改了文件")); 
  148.                     obj->m_listAllInfo.SetItemText(0,3,str1); 
  149.                     obj->m_listAllInfo.SetItemText(0,1,strTT); 
  150.                      
  151.                     pCheckInfo PI = NULL; 
  152.                     PI = new tagCheckInfo(); 
  153.                     strcpy(PI->szIndex,obj->m_pszListIndex); 
  154.                     strcpy(PI->szTime,strTT); 
  155.                     strcpy(PI->szType,_T("修改了文件")); 
  156.                     strcpy(PI->szDetail,str1); 
  157.                     obj->m_vecCI.push_back(PI); 
  158.                 } 
  159.                 break;   
  160.  
  161.             default
  162.                 if(obj->m_bOther) 
  163.                 { 
  164.                     CTime tt = CTime::GetCurrentTime();  
  165.                     CString strTT; 
  166.                     strTT.Format(_T("%d:%d:%d"),tt.GetHour(),tt.GetMinute(),tt.GetSecond()); 
  167.                     obj->m_listAllInfo.InsertItem(0,obj->m_pszListIndex); 
  168.                     obj->m_listAllInfo.SetItemText(0,2,_T("未知变化")); 
  169.                     obj->m_listAllInfo.SetItemText(0,3,_T("")); 
  170.                     obj->m_listAllInfo.SetItemText(0,1,strTT); 
  171.  
  172.                     pCheckInfo PI = NULL; 
  173.                     PI = new tagCheckInfo(); 
  174.                     strcpy(PI->szIndex,obj->m_pszListIndex); 
  175.                     strcpy(PI->szTime,strTT); 
  176.                     strcpy(PI->szType,_T("未知变化")); 
  177.                     strcpy(PI->szDetail,_T("")); 
  178.                     obj->m_vecCI.push_back(PI); 
  179.                 } 
  180.                 break;   
  181.             }    
  182.             obj->m_nListCount++;         
  183.             itoa(obj->m_nListCount,obj->m_pszListIndex,10);      
  184.         } 
  185.         else            
  186.             break;            
  187.     } 
  188.     obj->m_nListCount = 1; 
  189.     return 0; 

 

 

你可能感兴趣的:(监视文件,监视目录变化)