→ hiasdasz:謝謝大大,已解決^^ 02/17 10:48
※ 引述《hiasdasz (翻滾吧!大貓)》之銘言:
: 請教各位大大
: 目前使用FileSystemWatcher監聽資料夾
: 當資料夾內有文件建立或刪除
: 即顯示對話視窗(測試)
: 是否可以利用timer方式處理FileSystemWatcher的事件
: 例如:
: 每1分鐘內資料夾內是否有資料新增
: 有->A動作
: 無->B動作
: 就一分鐘檢查一次,不要一新增就動作
: 是否可以用timer和FileSystemWatcher實現?
: 還是有其他比較好的方法呢?
: 下面是我的程式碼
: namespace Works
: {
: public partial class Form1 : Form
: {
: FileSystemWatcher Wc = new FileSystemWatcher();
: public Form1()
: {
: InitializeComponent();
MyFileSystemWatcher(); //改在外部初始
: timer1.Interval = 1000 * 60;
: }
: private void timer1_Tick(object sender, EventArgs e)
: {
: //MyFileSystemWatcher();
//時間一到,判斷新增的Dictionary以及刪除的Dictionary內
//有沒有資料, 有的話將之秀出之後清除
//清除時請用 Lock{ } 限制住
: }
: private void MyFileSystemWatcher()
: {
: Wc.Path = @"C:\";
: Wc.Filter = "*.TXT";
: Wc.IncludeSubdirectories = true;
: Wc.EnableRaisingEvents = true;
: Wc.Created += new FileSystemEventHandler(Wc_Created);
: Wc.Deleted += new FileSystemEventHandler(Wc_Deleted);
: }
: private void Wc_Created(object sender, FileSystemEventArgs e)
: {
: //MessageBox.Show("有檔案被新增");
//可用一Dictionary物件記錄新增了哪一些檔案
//因為此處是另一Thread觸發,所以新增資料於Dictionary物件時
//請用 Lock { } 限制住
: }
: private void Wc_Deleted(object sender, FileSystemEventArgs e)
: {
: //MessageBox.Show("有檔案被刪除");
// 同新增作法
: }
: }
: }
: 請各位大大指教....
僅供參考 ...
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.39.183.197