c++文件監(jiān)控之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();
//創(chuàng)建一個(gè)FileSystemWatcher并設(shè)置它的屬性.
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>(//監(jiān)聽(tīng)文件的以下屬性 按需求添加 這里我添加了一些常用的
NotifyFilters::LastAccess | //文件或文件夾上一次打開(kāi)的日期。
NotifyFilters::LastWrite | //上一次向文件或文件夾寫(xiě)入內(nèi)容的日期
NotifyFilters::FileName | //文件名
NotifyFilters::DirectoryName | //目錄名
NotifyFilters::Size); //大小
//監(jiān)聽(tīng)子目錄
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();
}
過(guò)程 1.首先創(chuàng)建FileSystemWatcher 對(duì)象 用來(lái)設(shè)置一些屬性以及添加監(jiān)聽(tīng)事件
2.設(shè)置監(jiān)聽(tīng)目錄
3.設(shè)置監(jiān)聽(tīng)文件的屬性
4.設(shè)置監(jiān)聽(tīng)子目錄
5.添加監(jiān)聽(tīng)事件
6.開(kāi)始監(jiān)聽(tīng)
上面的sample代碼可以在MSDN上找到,如果有不確定的地方,可以查看文檔
相關(guān)文章
深入了解C++ 結(jié)構(gòu)體(struct)與共用體(union)
這篇文章主要介紹了C++ 結(jié)構(gòu)體與共用體的的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)c++,感興趣的朋友可以了解下2020-08-08
C++?STL標(biāo)準(zhǔn)庫(kù)之std::list使用介紹及用法詳解
std::list是支持常數(shù)時(shí)間從容器任何位置插入和移除元素的容器,下面這篇文章主要給大家介紹了關(guān)于C++?STL標(biāo)準(zhǔn)庫(kù)之std::list使用介紹及用法詳解的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11
Qt實(shí)現(xiàn)簡(jiǎn)單動(dòng)態(tài)時(shí)鐘
這篇文章主要為大家詳細(xì)介紹了Qt實(shí)現(xiàn)簡(jiǎn)單動(dòng)態(tài)時(shí)鐘,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07
C++動(dòng)態(tài)調(diào)用動(dòng)態(tài)鏈接庫(kù)(DLL或SO)的方法實(shí)現(xiàn)
動(dòng)態(tài)鏈接庫(kù)是一種Windows操作系統(tǒng)下常見(jiàn)的可執(zhí)行文件格式,它包含了一些可被其他應(yīng)用程序調(diào)用的函數(shù)和數(shù)據(jù),本文主要介紹了C++動(dòng)態(tài)調(diào)用動(dòng)態(tài)鏈接庫(kù)(DLL或SO),感興趣的可以了解一下2024-01-01
C++ 漢諾塔問(wèn)題知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家整理的是關(guān)于C++ 漢諾塔問(wèn)題知識(shí)點(diǎn)內(nèi)容,有需要的朋友們可以參考下。2020-02-02
C語(yǔ)言實(shí)現(xiàn)通訊錄的詳細(xì)代碼
本文詳細(xì)講解了C語(yǔ)言實(shí)現(xiàn)通訊錄的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12
stl容器set,map,vector之erase用法與返回值詳細(xì)解析
在使用 list、set 或 map遍歷刪除某些元素時(shí)可以這樣使用,如下所示2013-09-09

