2009-02-02 81 views

回答

2

如果您希望Windows告訴您特定的文件或子目錄已更改,您將需要使用ReadDirectoryChangesW。如果使用完成例程,則異步模式非常簡單。

另一方面,使用稍微複雜的I/O completion ports方法可能會獲得更好的性能。我會建議下載Wes Jones的優秀CDirectoryChangeWatcher源代碼作爲起點。他的代碼有幾個問題可以幫助您避免,特別是在解析FILE_NOTIFY_INFORMATION記錄時。

0

〜僞

HANDLE handles[MAX_HANDLES]; 
std::string dir_array[MAX_HANDLES]; 

for i from 0 to MAX_HANDLES: 
    h[i] = FindFirstChangeNotification(dir_array[i]...); 

nCount = MAX_HANDLES; 
ret = WaitForMultipleObjects(handles, nCount ...); 

// check if ret returns something between WAIT_OBJECT_0 and WAIT_OBJECT_0+nCount-1 
if "so": 
    ret -= WAIT_OBJECT_0; 
    cout << "Directory " << dir_array[ret] << " changed" << endl; 

參見:http://msdn.microsoft.com/en-us/library/ms687025(VS.85).aspx

0

參見專業Win32 API的新聞組消息://comp.os.ms-windows.programmer.win32 爲衆所周知的MS的樣品(C代碼)(自90年代起...)

相關問題