用FileSystemWatcher监视目标文件,如果被修改,立马读取,而不用考虑盲目读取了
FileSystemWatcher fileSystemWatcher1;
//开始监视
private void Init()
{
FileSystemWatcher fileSystemWatcher1 =new FileSystemWatcher();
this.fileSystemWatcher1.Created += new FileSystemEventHandler(fileSystemWatcher1_Created);
this.fileSystemWatcher1.Changed += new FileSystemEventHandler(fileSystemWatcher1_Changed);
this.fileSystemWatcher1.EnableRaisingEvents = true;
this.fileSystemWatcher1.Path = "监视路径";
this.fileSystemWatcher1.Filter = "*.txt";
this.fileSystemWatcher1.IncludeSubdirectories = false;//不监视子目录
}
private void fileSystemWatcher1_Changed(object sender, e)
{
if(e.FullPath=="目标文件")
{
//监视到目标文件被修改
}
}
private void fileSystemWatcher1_Created(object sender, e)
{
if(e.FullPath=="目标文件")
{
//监视到目标文件被创建
}
}
[
本帖最后由 xydddaxia 于 2011-9-28 13:36 编辑 ]