作者scottzz (我还是想离职休息一阵子)
看板C_Sharp
标题Re: [问题] 如何利用timer控制FileSystemWatcher的 …
时间Tue Feb 15 23:50:02 2011
※ 引述《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
1F:→ hiasdasz:谢谢大大,已解决^^ 02/17 10:48