c++文件监控之FileSystemWatcher
- 更新时间:2022-10-18 08:58:18
- 编辑:步小星
具体代码如下:
#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++的教程,这篇文章主要介绍了C++实现优酷土豆去视频广告的方法,实例分析了C++实现屏蔽功能的相关技巧,需要的朋友可以参考下
发布时间:2022-09-06
-
C++标准模板库函数sort的那些事儿
为网友们分享了关于C++的教程,sort函数是标准模板库的函数,已知开始和结束的地址即可进行排序,可以用于比较任何容器(必须满足随机迭代器),任何元素,任何条件,执行速度一般比qsort要快
发布时间:2022-07-11
-
Inline Hook(ring3)的简单C++实现方法
给大家整理了关于C++的教程,这篇文章主要介绍了Inline Hook(ring3)的简单C++实现方法,需要的朋友可以参考下
发布时间:2022-09-23
-
浅谈C++函数声明后面加throw()的作用(必看)
下面小编就为大家带来一篇浅谈C++函数声明后面加throw()的作用(必看)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
发布时间:2022-04-14
-
c++读取数据文件到数组的实例
今天小编就为大家分享一篇c++读取数据文件到数组的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
发布时间:2021-05-11
-
C++标准库中sstream与strstream的区别点总结
以下是对C++标准库中sstream与strstream的区别进行了详细的分析介绍,需要的朋友可以过来参考下
发布时间:2021-06-08
-
基于C++实现的哈夫曼编码解码操作示例
给网友朋友们带来一篇关于C++的教程,这篇文章主要介绍了基于C++实现的哈夫曼编码解码操作,结合实例形式分析了C++实现的哈夫曼编码解码相关定义与使用技巧,需要的朋友可以参考下
发布时间:2022-10-09
-
详解C++中的双冒号 ::
给网友朋友们带来一篇关于C++的教程,这篇文章主要介绍了C++中的双冒号 ::,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
发布时间:2022-09-06