您的位置:首页 > 其它

使用控制线程监视目录文件变化

2007-07-27 09:11 344 查看

using System;


using System.IO;


using System.Xml;


using System.Collections.Generic;


using System.ComponentModel;


using System.Data;


using System.Drawing;


using System.Text;


using System.Windows.Forms;


using System.Threading;


using System.Diagnostics;


using SynchroManager;


using SynchroManager.baseclass;








namespace SynchroManager




...{


public partial class frmChange : Form




...{


public frmChange()




...{


InitializeComponent();


}


delegate void SetTextCallback(string text);




private string s;


private string _dowlist;


private string _LookFolder;


private Thread demoThread = null;




private void btnRun_Click(object sender, EventArgs e)




...{


FileSystemWatcher watcher = new FileSystemWatcher();


if (txtFilter.Text.Length < 2)




...{


MessageBox.Show("请选择监视目录");


return;


}




watcher.Path = txtFilter.Text;


watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;




watcher.Filter = "";






watcher.Changed += new FileSystemEventHandler(OnChanged);


watcher.Created += new FileSystemEventHandler(OnCreated);


watcher.Deleted += new FileSystemEventHandler(OnDeleted);


watcher.Renamed += new RenamedEventHandler(OnRenamed);


watcher.EnableRaisingEvents = true;


watcher.EndInit();




}




private void OnChanged(object source, FileSystemEventArgs e)




...{


s = e.FullPath + " " + e.ChangeType;


this.demoThread = new Thread(new ThreadStart(this.ThreadProcSafe));


this.demoThread.Start();


//StreamWriter strW = File.CreateText("C:/Inetpub/ftproot/downlist.txt");


//strW.WriteLine(s);


//strW.Close();






}


private void OnCreated(object source, FileSystemEventArgs e)




...{


s = e.FullPath + " " + e.ChangeType;


this.demoThread = new Thread(new ThreadStart(this.ThreadProcSafe));


this.demoThread.Start();


//StreamWriter strW = File.CreateText("C:/Inetpub/ftproot/downlist.txt");


//strW.WriteLine(s);


//strW.Close();




}


private void OnDeleted(object source, FileSystemEventArgs e)




...{


s = e.FullPath + " " + e.ChangeType;


this.demoThread = new Thread(new ThreadStart(this.ThreadProcSafe));


this.demoThread.Start();


//StreamWriter strW = File.CreateText("C:/Inetpub/ftproot/downlist.txt");


//strW.WriteLine(s);


//strW.Close();




}




private void OnRenamed(object source, FileSystemEventArgs e)




...{


s = e.FullPath + " " + e.ChangeType;


this.demoThread = new Thread(new ThreadStart(this.ThreadProcSafe));


this.demoThread.Start();




//StreamWriter strW = File.CreateText("C:/Inetpub/ftproot/downlist.txt");


//while((st=strR.ReadLine())!=null;


//strW.WriteLine(s);


//strW.Close();




}




private void SetText(string text)
{
if (this.lblist.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.lblist.Items.Add(text);
}
}
private void ThreadProcSafe()
{
this.SetText(s);
}




}


}


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: