c++文件监控之FileSystemWatcher

所属分类: 软件编程 / C 语言 阅读数: 41
收藏 0 赞 0 分享

具体代码如下:

#using <System.dll>
#include <iostream>

using namespace std;
using namespace System;
using namespace System::IO;
using namespace System::Security::Permissions;

public ref class Watcher
{
private:
  // Define the event handlers.
  static void OnChanged( Object^ /*source*/, FileSystemEventArgs^ e )
  {
   // Specify what is done when a file is changed, created, or deleted.
   Console::WriteLine( "File: {0} {1}", e->FullPath, e->ChangeType );
  }

  static void OnRenamed( Object^ /*source*/, RenamedEventArgs^ e )
  {
   // Specify what is done when a file is renamed.
   Console::WriteLine( "File: {0} renamed to {1}", e->OldFullPath, e->FullPath );
  }

public:
  [PermissionSet(SecurityAction::Demand, Name="FullTrust")]
  int static run()
  {
   //array<String^>^args = System::Environment::GetCommandLineArgs();
   //创建一个FileSystemWatcher并设置它的属性.
   FileSystemWatcher^ fsWatcher = gcnew FileSystemWatcher( );
   fsWatcher->Path = "C:\\files";

   /* Watch for changes in LastAccess and LastWrite times, and
     the renaming of files or directories. */
   fsWatcher->NotifyFilter = static_cast<NotifyFilters>(//监听文件的以下属性 按需求添加 这里我添加了一些常用的
                NotifyFilters::LastAccess | //文件或文件夹上一次打开的日期。 
                NotifyFilters::LastWrite | //上一次向文件或文件夹写入内容的日期
                NotifyFilters::FileName | //文件名
                NotifyFilters::DirectoryName | //目录名
                NotifyFilters::Size); //大小

   //监听子目录
   fsWatcher->IncludeSubdirectories = true;
   // Only watch text files.
   //fsWatcher->Filter = "*.txt";

   // Add event handlers.
   fsWatcher->Changed += gcnew FileSystemEventHandler( Watcher::OnChanged );
   fsWatcher->Created += gcnew FileSystemEventHandler( Watcher::OnChanged );
   fsWatcher->Deleted += gcnew FileSystemEventHandler( Watcher::OnChanged );
   fsWatcher->Renamed += gcnew RenamedEventHandler( Watcher::OnRenamed );

   // Begin watching.
   fsWatcher->EnableRaisingEvents = true;

   // Wait for the user to quit the program.
   Console::WriteLine( "Press \'q\' to quit the sample." );
   while ( Console::Read() != 'q' );

   return 0;
  }
};

int main() {
  Watcher::run();
}

过程 1.首先创建FileSystemWatcher 对象  用来设置一些属性以及添加监听事件

   2.设置监听目录

   3.设置监听文件的属性

   4.设置监听子目录

   5.添加监听事件

   6.开始监听

上面的sample代码可以在MSDN上找到,如果有不确定的地方,可以查看文档

更多精彩内容其他人还在看

C语言非递归后序遍历二叉树

这篇文章主要为大家详细介绍了C语言非递归后序遍历二叉树,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

C语言单链表实现多项式相加

这篇文章主要为大家详细介绍了C语言单链表实现多项式相加,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

C语言二叉排序(搜索)树实例

这篇文章主要为大家详细介绍了C语言二叉排序树实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

NDK 数据结构之队列与栈等的实现

这篇文章主要介绍了NDK 数据结构之队列与栈等的实现的相关资料,希望通过本文大家能理解掌握这部分内容,需要的朋友可以参考下
收藏 0 赞 0 分享

C/C++经典实例之模拟计算器示例代码

最近在看到的一个需求,本以为比较简单,但花了不少时间,所以下面这篇文章主要给大家介绍了关于C/C++经典实例之模拟计算器的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

C语言中的getchar和putchar的使用方法

这篇文章主要介绍了C语言中的getchar和putchar的使用方法的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下
收藏 0 赞 0 分享

C++实现洗牌发牌排序功能的示例代码

本篇文章主要介绍了C++实现洗牌发牌排序功能的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

C++计算图任意两点间的所有路径

这篇文章主要为大家详细介绍了C++求图任意两点间的所有路径 ,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

zlib库压缩和解压字符串STL string的实例详解

这篇文章主要介绍了zlib库压缩和解压字符串STL string的实例详解的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下
收藏 0 赞 0 分享

C/C++ 获取Windows系统的位数32位或64位的实现代码

这篇文章主要介绍了C/C++ 获取Windows系统的位数32位或64位的实现代码的相关资料,希望通过本文能帮助到大家,让大家实现这样的功能,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多