怎么监测文件系统的变化(不使用轮循的方法)

ghw 2001-12-04 02:22:48
在一台机器上,我怎么发现它的文件和目录的建立、删除和修改操作?

不能使用轮循的方法检查文件和目录的lastModify时间,因为效率太低下了。

不知道能不能在这些操作发生的时候就监测到并记录下来?
...全文
175 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
ghw 2001-12-10
  • 打赏
  • 举报
回复
大家再说几句,下午5点给分。
ghw 2001-12-10
  • 打赏
  • 举报
回复
散分
tpProgramer 2001-12-06
  • 打赏
  • 举报
回复
Windows可以检测到文件变化的,比如你在打开FileOpen对话框时,又往那个文件夹添加了一个文件,在FileOpen对话框中就会显示出来,你可以看看帮助的。我这里贴一段代码:
希望对你有帮助:

DWORD dwWaitStatus;
HANDLE dwChangeHandles[2];

// Watch the C:\WINDOWS directory for file creation and
// deletion.

dwChangeHandles[0] = FindFirstChangeNotification(
"C:\\WINDOWS", // directory to watch
FALSE, // do not watch the subtree
FILE_NOTIFY_CHANGE_FILE_NAME); // watch file name changes

if (dwChangeHandles[0] == INVALID_HANDLE_VALUE)
ExitProcess(GetLastError());

// Watch the C:\ subtree for directory creation and
// deletion.

dwChangeHandles[1] = FindFirstChangeNotification(
"C:\\", // 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 C:\WINDOWS.
// Refresh this directory and restart the
// change notification. RefreshDirectory is an
// application-defined function.

RefreshDirectory("C:\\WINDOWS")
if ( FindNextChangeNotification(
dwChangeHandles[0]) == FALSE )
ExitProcess(GetLastError());
break;

case WAIT_OBJECT_0 + 1:

// A directory was created or deleted in C:\.
// Refresh the directory tree and restart the
// change notification. RefreshTree is an
// application-defined function.

RefreshTree("C:\\");
if (FindNextChangeNotification(
dwChangeHandles[1]) == FALSE)
ExitProcess(GetLastError());
break;

default:
ExitProcess(GetLastError());
}
}
ghw 2001-12-06
  • 打赏
  • 举报
回复
to NowCan(能量、激情、雨水、彩虹——雷雨云):
我到你的主页看了,首页的左边那些图片出不来,上面也找不到链接,你看看是不是有问题了。
ghw 2001-12-06
  • 打赏
  • 举报
回复
谢谢tpProgramer(tp编程者)。

我想问一下Linux/Unix下怎么实现,因为多种平台下都要用。
NowCan 2001-12-05
  • 打赏
  • 举报
回复
还有一个函数,不知是否有用。
ReadDirectoryChangesW
NowCan 2001-12-05
  • 打赏
  • 举报
回复
FindFirstChangeNotification
FindNextChangeNotification
我主页上有个历程,是VB写的,你可以参考一下。
http://nowcan.yeah.net
goldsun 2001-12-05
  • 打赏
  • 举报
回复
不好意思,我不会
ghw 2001-12-05
  • 打赏
  • 举报
回复
各位高手,给点提示不行吗?

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧