c++文件监控之FileSystemWatcher
2019/7/10 22:27:47
本文主要是介绍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上找到,如果有不确定的地方,可以查看文档
这篇关于c++文件监控之FileSystemWatcher的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-15PingCAP 黄东旭参与 CCF 秀湖会议,共探开源教育未来
- 2024-05-13PingCAP 戴涛:构建面向未来的金融核心系统
- 2024-05-09flutter3.x_macos桌面os实战
- 2024-05-09Rust中的并发性:Sync 和 Send Traits
- 2024-05-08使用Ollama和OpenWebUI在CPU上玩转Meta Llama3-8B
- 2024-05-08完工标准(DoD)与验收条件(AC)究竟有什么不同?
- 2024-05-084万 star 的 NocoDB 在 sealos 上一键起,轻松把数据库编程智能表格
- 2024-05-08Mac 版Stable Diffusion WebUI的安装
- 2024-05-08解锁CodeGeeX智能问答中3项独有的隐藏技能
- 2024-05-08RAG算法优化+新增代码仓库支持,CodeGeeX的@repo功能效果提升