作者hiasdasz (翻滾吧!大貓)
看板C_Sharp
標題[問題] 如何利用timer控制FileSystemWatcher的事件?
時間Tue Feb 15 14:48:51 2011
請教各位大大
目前使用FileSystemWatcher監聽資料夾
當資料夾內有文件建立或刪除
即顯示對話視窗(測試)
是否可以利用timer方式處理FileSystemWatcher的事件
例如:
每1分鐘內資料夾內是否有資料新增
有->A動作
無->B動作
就一分鐘檢查一次,不要一新增就動作
是否可以用timer和FileSystemWatcher實現?
還是有其他比較好的方法呢?
下面是我的程式碼
namespace Works
{
public partial class Form1 : Form
{
FileSystemWatcher Wc = new FileSystemWatcher();
public Form1()
{
InitializeComponent();
timer1.Interval = 1000 * 60;
}
private void timer1_Tick(object sender, EventArgs e)
{
MyFileSystemWatcher();
}
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("有檔案被新增");
}
private void Wc_Deleted(object sender, FileSystemEventArgs e)
{
MessageBox.Show("有檔案被刪除");
}
}
}
請各位大大指教....
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 122.121.25.20
1F:→ scottzz:這樣寫,第一次Timer進去就初始FileSystemWatcher了 02/15 22:56
2F:→ scottzz:之後每次有檔案進來就會觸發了 02/15 22:57
3F:→ hiasdasz:那請問樓上大大,我該如何寫? 事件要False嗎? 02/15 23:18
4F:→ yeo1987:以變數紀錄時間, 觸發時檢查時間是否過一分鐘 02/16 23:52
5F:→ yeo1987:沒過一分鐘不理, 過一分鐘即將現在時間寫入變數 02/16 23:53
6F:→ yeo1987:, 再執行想要的動作就好了. 02/16 23:54
7F:→ hiasdasz:謝謝各位大大,已解決^^ 02/17 10:48