c#/c++ 通過(guò)系統(tǒng)api-FileSystemWatcher監(jiān)視文件或目錄變化的問(wèn)題
當(dāng)前位置:點(diǎn)晴教程→知識(shí)管理交流
→『 技術(shù)文檔交流 』
1. using System; 2. using System.IO; 3. 4. class Program 5. { 6. static void Main() 7. { 8. // 要監(jiān)視的目錄路徑 9. string pathToWatch = @"C:\Path\To\Your\Directory"; 10. 11. // 創(chuàng)建一個(gè)新的 FileSystemWatcher 實(shí)例 12. using (FileSystemWatcher watcher = new FileSystemWatcher()) 13. { 14. // 設(shè)置要監(jiān)視的目錄 15. watcher.Path = pathToWatch; 16. 17. // 設(shè)置要監(jiān)視的文件和目錄的更改類型 18. watcher.NotifyFilter = NotifyFilters.LastWrite 19. | NotifyFilters.FileName 20. | NotifyFilters.DirectoryName; 21. //包含子目錄監(jiān)視 22. watcher.IncludeSubdirectories = true; 23. // 啟用事件引發(fā) 24. watcher.EnableRaisingEvents = true; 25. 26. // 添加事件處理程序 27. watcher.Created += OnCreated; 28. watcher.Deleted += OnDeleted; 29. watcher.Changed += OnChanged; 30. watcher.Renamed += OnRenamed; 31. 32. // 監(jiān)視狀態(tài) 33. Console.WriteLine($"正在監(jiān)視目錄:{pathToWatch}"); 34. Console.WriteLine("按任意鍵退出."); 35. Console.ReadKey(); 36. } 37. } 38. 39. // 文件或目錄創(chuàng)建事件處理程序 40. private static void OnCreated(object sender, FileSystemEventArgs e) 41. { 42. Console.WriteLine($"新文件或目錄已創(chuàng)建: {e.Name} - 類型: {e.ChangeType}"); 43. } 44. 45. // 文件或目錄刪除事件處理程序 46. private static void OnDeleted(object sender, FileSystemEventArgs e) 47. { 48. Console.WriteLine($"文件或目錄已刪除: {e.Name} - 類型: {e.ChangeType}"); 49. } 50. 51. // 文件或目錄更改事件處理程序 52. private static void OnChanged(object sender, FileSystemEventArgs e) 53. { 54. Console.WriteLine($"文件或目錄已更改: {e.Name} - 類型: {e.ChangeType}"); 55. } 56. 57. // 文件或目錄重命名事件處理程序 58. private static void OnRenamed(object sender, RenamedEventArgs e) 59. { 60. Console.WriteLine($"文件或目錄已重命名: {e.OldName} -> {e.Name} - 類型: {e.ChangeType}"); 61. } 62.} 1. #include <windows.h> 2. #include <stdlib.h> 3. #include <stdio.h> 4. #include <tchar.h> 5. 6. void RefreshDirectory(LPTSTR); 7. void RefreshTree(LPTSTR); 8. void WatchDirectory(LPTSTR); 9. 10.void _tmain(int argc, TCHAR *argv[]) 11.{ 12. if(argc != 2) 13. { 14. _tprintf(TEXT("Usage: %s <dir>\n"), argv[0]); 15. return; 16. } 17. 18. WatchDirectory(argv[1]); 19.} 20. 21.void WatchDirectory(LPTSTR lpDir) 22.{ 23. DWORD dwWaitStatus; 24. HANDLE dwChangeHandles[2]; 25. TCHAR lpDrive[4]; 26. TCHAR lpFile[_MAX_FNAME]; 27. TCHAR lpExt[_MAX_EXT]; 28. 29. _tsplitpath_s(lpDir, lpDrive, 4, NULL, 0, lpFile, _MAX_FNAME, lpExt, _MAX_EXT); 30. 31. lpDrive[2] = (TCHAR)'\\'; 32. lpDrive[3] = (TCHAR)'\0'; 33. 34.// Watch the directory for file creation and deletion. 35. 36. dwChangeHandles[0] = FindFirstChangeNotification( 37. lpDir, // directory to watch 38. FALSE, // do not watch subtree 39. FILE_NOTIFY_CHANGE_FILE_NAME); // watch file name changes 40. 41. if (dwChangeHandles[0] == INVALID_HANDLE_VALUE) 42. { 43. printf("\n ERROR: FindFirstChangeNotification function failed.\n"); 44. ExitProcess(GetLastError()); 45. } 46. 47.// Watch the subtree for directory creation and deletion. 48. 49. dwChangeHandles[1] = FindFirstChangeNotification( 50. lpDrive, // directory to watch 51. TRUE, // watch the subtree 52. FILE_NOTIFY_CHANGE_DIR_NAME); // watch dir name changes 53. 54. if (dwChangeHandles[1] == INVALID_HANDLE_VALUE) 55. { 56. printf("\n ERROR: FindFirstChangeNotification function failed.\n"); 57. ExitProcess(GetLastError()); 58. } 59. 60. 61.// Make a final validation check on our handles. 62. 63. if ((dwChangeHandles[0] == NULL) || (dwChangeHandles[1] == NULL)) 64. { 65. printf("\n ERROR: Unexpected NULL from FindFirstChangeNotification.\n"); 66. ExitProcess(GetLastError()); 67. } 68. 69.// Change notification is set. Now wait on both notification 70.// handles and refresh accordingly. 71. 72. while (TRUE) 73. { 74. // Wait for notification. 75. 76. printf("\nWaiting for notification...\n"); 77. 78. dwWaitStatus = WaitForMultipleObjects(2, dwChangeHandles, 79. FALSE, INFINITE); 80. 81. switch (dwWaitStatus) 82. { 83. case WAIT_OBJECT_0: 84. 85. // A file was created, renamed, or deleted in the directory. 86. // Refresh this directory and restart the notification. 87. 88. RefreshDirectory(lpDir); 89. if ( FindNextChangeNotification(dwChangeHandles[0]) == FALSE ) 90. { 91. printf("\n ERROR: FindNextChangeNotification function failed.\n"); 92. ExitProcess(GetLastError()); 93. } 94. break; 95. 96. case WAIT_OBJECT_0 + 1: 97. 98. // A directory was created, renamed, or deleted. 99. // Refresh the tree and restart the notification. 100. 101. RefreshTree(lpDrive); 102. if (FindNextChangeNotification(dwChangeHandles[1]) == FALSE ) 103. { 104. printf("\n ERROR: FindNextChangeNotification function failed.\n"); 105. ExitProcess(GetLastError()); 106. } 107. break; 108. 109. case WAIT_TIMEOUT: 110. 111. // A timeout occurred, this would happen if some value other 112. // than INFINITE is used in the Wait call and no changes occur. 113. // In a single-threaded environment you might not want an 114. // INFINITE wait. 115. 116. printf("\nNo changes in the timeout period.\n"); 117. break; 118. 119. default: 120. printf("\n ERROR: Unhandled dwWaitStatus.\n"); 121. ExitProcess(GetLastError()); 122. break; 123. } 124. } 125. } 126. 127. void RefreshDirectory(LPTSTR lpDir) 128. { 129. // This is where you might place code to refresh your 130. // directory listing, but not the subtree because it 131. // would not be necessary. 132. 133. _tprintf(TEXT("Directory (%s) changed.\n"), lpDir); 134. } 135. 136. void RefreshTree(LPTSTR lpDrive) 137. { 138. // This is where you might place code to refresh your 139. // directory listing, including the subtree. 140. 141. _tprintf(TEXT("Directory tree (%s) changed.\n"), lpDrive); 142. } 該文章在 2023/12/26 22:12:56 編輯過(guò) |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |