2009-02-02 84 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年代起...)

相关问题