通過以下兩個類,可以實現對windows資料夾的事件偵聽,包括新建檔案,刪除檔案,檔案重新命名等事件;
filesystemwather.cs
using system;
using system.collections;
using system.io;
using system.threading;
namespace whir.software.filecopy
_hstbwather = new hashtable();
_fswather = new filesystemwatcher(path) ;
_fswather.renamed += fswather_renamed;
_fswather.changed += fswather_changed;
_fswather.created += fswather_created;
_fswather.deleted += fswather_deleted;
}public event renamedeventhandler onrenamed;
public event filesystemeventhandler onchanged;
public event filesystemeventhandler oncreated;
public event filesystemeventhandler ondeleted;
/// /// 開始監控
///
public void start()
/// /// 停止監控
///
public void stop()
/// /// filesystemwatcher 本身的事件通知處理過程
///
///
///
private void fswather_renamed(object sender, renamedeventargs e)
}var watcherprocess = new watcherprocess(sender, e);
watcherprocess.onrenamed += onrenamed; //傳遞事件
watcherprocess.oncompleted += watcherprocess_oncompleted;
var thread = new thread(watcherprocess.process);
thread.start();
}private void fswather_created(object sender, filesystemeventargs e)
}var watcherprocess = new watcherprocess(sender, e);
watcherprocess.oncreated += oncreated; //傳遞事件
watcherprocess.oncompleted += watcherprocess_oncompleted;
var threaddeal = new thread(watcherprocess.process);
threaddeal.start();
}private void fswather_deleted(object sender, filesystemeventargs e)
}var watcherprocess = new watcherprocess(sender, e);
watcherprocess.ondeleted += ondeleted; //傳遞事件
watcherprocess.oncompleted += watcherprocess_oncompleted;
var tddeal = new thread(watcherprocess.process);
tddeal.start();
}private void fswather_changed(object sender, filesystemeventargs e)}}
lock (_hstbwather)
}var watcherprocess = new watcherprocess(sender, e);
watcherprocess.onchanged += onchanged; //傳遞事件
watcherprocess.oncompleted += watcherprocess_oncompleted;
var thread = new thread(watcherprocess.process);
thread.start();
}/// /// 使用了執行緒安全的hashtable來處理一次改變觸發兩次事件的問題,
/// 要注意的是在實際專案使用中,在通過監控檔案事情觸發時開乙個執行緒watcherprocess去處理自己業務邏輯的時候,
/// 不管業務邏輯成功或者失敗(例如有異常丟擲一定要try一下)一定要讓watcherprocess的completed
/// 也就是myfilesystemwather的watcherprocess_oncompleted執行去移除對應變化檔案的hashtable的key,
/// 不然下次此檔案改變時是無法觸發你的業務邏輯的。
///
///
public void watcherprocess_oncompleted(string key)}}
}
watcherprocess.cs
using system.io;
namespace whir.software.filecopy
public event renamedeventhandler onrenamed;
public event filesystemeventhandler onchanged;
public event filesystemeventhandler oncreated;
public event filesystemeventhandler ondeleted;
public event completed oncompleted;
public void process()
else
else if (e.changetype == watcherchangetypes.changed)
else if (e.changetype == watcherchangetypes.deleted)
else}}
}}
使用方法:
var wather = new filesystemwather(txtfrom.text, "*.*", true);
wather.onchanged += onchanged;
wather.oncreated += oncreated;
wather.onrenamed += onrenamed;
wather.ondeleted += ondeleted;
wather.start();
對應事件響應:
private void oncreated(object source, filesystemeventargs e)
private void onchanged(object source, filesystemeventargs e)
private void ondeleted(object source, filesystemeventargs e)
private void onrenamed(object source, renamedeventargs e)
windows資料夾列表展示資料夾大小
通常情況下,檢視windows資源管理器中資料夾大小都需要右鍵檢視資料夾屬性,才可以看到,或者從資料夾選項中勾選滑鼠指向資料夾和桌面項時顯示提示資訊,這樣可以將滑鼠移動到資料夾上檢視資料夾的大小,但是這也不太方便,其實想要達到的效果是,在windows資源管理器中資料夾列表中可以直接展示資料夾大小。...
Windows資料夾加密
相信大家都遇到過自己的一些隱私檔案不願意讓別人看到的情況吧,怎麼解決呢?隱藏起來?換個名字?或者加密?這些辦法都可以辦到,其實還有一種方法,就是建立乙個別人既不能進入又不能刪除的資料夾,把自己的隱私檔案放進去,別人就看不到啦,下面講講如何實現,很簡單的。第一步 在執行中輸入cmd,回車,開啟命令列視...
Windows「啟動」資料夾
windows系統都有乙個 啟動 資料夾,把需要開啟的程式的快捷方式放到 啟動 資料夾裡,就可以實現開機自動啟動。啟動 資料夾分為兩種,一種是 系統啟動資料夾 另一種為 使用者啟動資料夾 win10系統 啟動 資料夾的路徑為 x programdata microsoft windows start...