c++文件监控之FileSystemWatcher

具体代码如下:

#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上找到,如果有不确定的地方,可以查看文档

(0)

相关推荐

  • C++与namespace有关的两个编译错误的讲解

    某次,在大型的工程代码中,我这样调用: #include <iostream> using namespace std; namespace A { void fun() { printf("aaa\n"); } } namespace B { void fun() { printf("bbb\n"); } } int main() { fun(); return 0; } 编译出错:error: 'fun' was not declared in th

  • C++中rapidjson组装map和数组array的代码示例

    rapidjson组装map和数组array的代码示例 直接上码: #include <iostream> #include <map> // 请自己下载开源的rapidjson #include "rapidjson/prettywriter.h" #include "rapidjson/rapidjson.h" #include "rapidjson/document.h" #include "rapidjs

  • C++类中变量也可以是引用的代码实例

    C++类中变量也可以是引用哈------要用初始化列表来初始化(因为C++引用一旦绑定,就无法更换,有点类似const) #include <iostream> using namespace std; class A { public: int &x; int &y; A(int &tmpX, int &tmpY):x(tmpX), y(tmpY){} }; int main() { int tmpX = 1; int tmpY = 2; A a(tmpX,

  • C++基类指针和派生类指针之间的转换方法讲解

    函数重载.函数隐藏.函数覆盖 函数重载只会发生在同作用域中(或同一个类中),函数名称相同,但参数类型或参数个数不同. 函数重载不能通过函数的返回类型来区分,因为在函数返回之前我们并不知道函数的返回类型. 函数隐藏和函数覆盖只会发生在基类和派生类之间. 函数隐藏是指派生类中函数与基类中的函数同名,但是这个函数在基类中并没有被定义为虚函数,这种情况就是函数的隐藏. 所谓隐藏是指使用常规的调用方法,派生类对象访问这个函数时,会优先访问派生类中的这个函数,基类中的这个函数对派生类对象来说是隐藏起来的.

  • C++直接cout指针名的含义?

    首先看下面这个代码实例: #include <iostream> using namespace std; int main() { char *str = "this is a test"; cout << "str=" << str << endl; cout << "*str=" << *str << endl; cout << "&a

  • strings命令分析浅谈Go和C++编译时的一点小区别

    最近查一个bug, 用strings命令分析, 竟然出乎意料地没有结果, 非常纳闷. 最后根据这个线索查出了bug的根本原因. 1.  在C++中, 即使函数在代码层面没有被调用, 也会最终编译到二进制中, 用strings可以分析.  #include <iostream> using namespace std; void fun() { printf("hello world\n"); // strings分析有结果 } int main() { return 0;

  • C++中rapidjson组装继续简化的方法

    rapidjson组装继续简化------人生苦短,我用rapidjson 看最简单的: #include <iostream> #include <stdio.h> #include<unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include<sstream> // 请自己下载开源的rapidjson #includ

  • 详解C++中虚析构函数的作用及其原理分析

    C++中的虚析构函数到底什么时候有用的,什么作用呢. 一.虚析构函数的作用 总的来说虚析构函数是为了避免内存泄露,而且是当子类中会有指针成员变量时才会使用得到的.也就说虚析构函数使得在删除指向子类对象的基类指针时可以调用子类的析构函数达到释放子类中堆内存的目的,而防止内存泄露的. 我们知道,用C++开发的时候,用来做基类的类的析构函数一般都是虚函数.可是,为什么要这样做呢?下面用一个小例子来说明: #include<iostream> using namespace std; class Cl

  • c++编写String类代码实例

    本文实例为大家分享了c++编写String类的具体代码,供大家参考,具体内容如下 class String { public: String(const char* = nullptr); //普通构造函数 String(const String& other); //拷贝构造函数 ~String(void); //析构函数 String& operator = (const String& other); //赋值函数 private: char* m_data; }; //普通

  • 用C/C++代码检测ip能否ping通(配合awk和system可以做到批量检测)

    遇到一个小需求, 快速搞定. 来看看用C/C++代码检测ip能否ping通: #include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> using namespace std; string getCmdResult(const string &strCmd) // 这个是获取命令执行的结果, 类似于

随机推荐