国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

c++文件監控之FileSystemWatcher

2020-01-26 13:29:22
字體:
來源:轉載
供稿:網友

具體代碼如下:

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 灵寿县| 广宁县| 武宣县| 永泰县| 兰溪市| 白水县| 靖宇县| 浮梁县| 来凤县| 金阳县| 镇沅| 日照市| 青阳县| 西林县| 南澳县| 崇明县| 绥阳县| 林州市| 宝鸡市| 稷山县| 武宣县| 玉门市| 宿迁市| 城口县| 革吉县| 浮山县| 滕州市| 武陟县| 东山县| 三穗县| 黄龙县| 三穗县| 永胜县| 赤壁市| 南召县| 和顺县| 麻阳| 泰顺县| 阳新县| 探索| 呼玛县|