使用控制线程监视目录文件变化
2007-07-27 09:11
344 查看
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.IO;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Xml;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Collections.Generic;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.ComponentModel;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Data;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Drawing;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Text;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Windows.Forms;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Threading;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Diagnostics;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using SynchroManager;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using SynchroManager.baseclass;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
namespace SynchroManager
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public partial class frmChange : Form
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public frmChange()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
InitializeComponent();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
delegate void SetTextCallback(string text);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private string s;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private string _dowlist;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private string _LookFolder;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private Thread demoThread = null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private void btnRun_Click(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FileSystemWatcher watcher = new FileSystemWatcher();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (txtFilter.Text.Length < 2)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MessageBox.Show("请选择监视目录");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
watcher.Path = txtFilter.Text;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
watcher.Filter = "";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
watcher.Changed += new FileSystemEventHandler(OnChanged);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
watcher.Created += new FileSystemEventHandler(OnCreated);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
watcher.Deleted += new FileSystemEventHandler(OnDeleted);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
watcher.Renamed += new RenamedEventHandler(OnRenamed);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
watcher.EnableRaisingEvents = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
watcher.EndInit();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private void OnChanged(object source, FileSystemEventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
s = e.FullPath + " " + e.ChangeType;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.demoThread = new Thread(new ThreadStart(this.ThreadProcSafe));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.demoThread.Start();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//StreamWriter strW = File.CreateText("C:/Inetpub/ftproot/downlist.txt");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//strW.WriteLine(s);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//strW.Close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private void OnCreated(object source, FileSystemEventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
s = e.FullPath + " " + e.ChangeType;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.demoThread = new Thread(new ThreadStart(this.ThreadProcSafe));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.demoThread.Start();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//StreamWriter strW = File.CreateText("C:/Inetpub/ftproot/downlist.txt");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//strW.WriteLine(s);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//strW.Close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private void OnDeleted(object source, FileSystemEventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
s = e.FullPath + " " + e.ChangeType;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.demoThread = new Thread(new ThreadStart(this.ThreadProcSafe));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.demoThread.Start();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//StreamWriter strW = File.CreateText("C:/Inetpub/ftproot/downlist.txt");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//strW.WriteLine(s);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//strW.Close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private void OnRenamed(object source, FileSystemEventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
s = e.FullPath + " " + e.ChangeType;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.demoThread = new Thread(new ThreadStart(this.ThreadProcSafe));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.demoThread.Start();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//StreamWriter strW = File.CreateText("C:/Inetpub/ftproot/downlist.txt");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//while((st=strR.ReadLine())!=null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//strW.WriteLine(s);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//strW.Close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
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);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
}
相关文章推荐
- 使用控制线程监视目录文件变化
- 使用线程监控文件目录变化
- java 使用线程监控文件目录变化的实现方法
- java 使用线程监控文件目录变化的实现方法
- 使用Python监视指定目录下文件变更的方法
- c#使用线程下载文件的控制技巧和缺陷
- 使用线程加载指定目录下的所有子目录和文件名称到TreeView中
- 使用inotify监视Linux文件变化
- 用QFileSystemWatcher来监视文件和目录的改变(内部还是使用了timer)
- 使用FileSystemWatcher监视文件变化
- 使用线程加载指定目录下的所有子目录和文件名称到TreeView中
- 使用apache htpasswd生成加密的密码文件,并使用.htaccess控制目录访问
- 使用线程加载指定目录下的所有子目录和文件名称到TreeView中
- .net 监视文件系统中目录和文件的变化
- c#使用filesystemwatcher监视文件系统的变化
- 使用FileSystemWatcher监视文件变化
- C#使用线程加载指定目录下的所有子目录和文件名称到TreeView中
- [Java 8] (4) 列举目录下的所有文件和监视文件变化
- C#~使用FileSystemWatcher来监视文件系统的变化
- 用inotify监视文件/目录变化