去年开发一个小型的网络系统时,编写了一个对指定目录的本地文件(实际上别人机器上共享文件也可以)进行遍历和 I/O 监控的控件,近来有人问及相关问题,就把他贴出来吧,很简单的程序,高手勿进哦 :)代码如下
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.IO;
using System.Threading;
namespace MyFtpWatcherControlLibrary
{
///
/// UserControl1 的摘要说明。
///
public class FtpFileWatcher : System.Windows.Forms.UserControl
{
private System.IO.FileSystemWatcher fileSystemWatcher;
private System.Windows.Forms.TextBox textBoxWatch;
private string SerIP, SerPort, VFolder, str_found, dirs;
private int num_found_files;
private Thread thread_ergodic;
///
/// 必需的设计器变量。
///
private System.ComponentModel.Container components = null;
public FtpFileWatcher()
{
// 该调用是 Windows.Forms 窗体设计器所必需的。
InitializeComponent();
// TODO: 在 InitComponent 调用后添加任何初始化
}
///
/// 清理所有正在使用的资源。
///
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
components.Dispose();
}
base.Dispose(disposing);
}
#region 组件设计器生成的代码
///
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
///
private void InitializeComponent()
{
this.fileSystemWatcher = new System.IO.FileSystemWatcher();
this.textBoxWatch = new System.Windows.Forms.TextBox();
((System.ComponentModel.ISupportInitialize)(this.fileSystemWatcher)).BeginInit();
this.SuspendLayout();
//
// fileSystemWatcher
//
this.fileSystemWatcher.IncludeSubdirectories = true;
this.fileSystemWatcher.NotifyFilter = ((System.IO.NotifyFilters)((System.IO.NotifyFilters.FileName | System.IO.NotifyFilters.DirectoryName)));
this.fileSystemWatcher.SynchronizingObject = this;
this.fileSystemWatcher.Deleted += new System.IO.FileSystemEventHandler(this.fileSystemWatcher_Changed);
this.fileSystemWatcher.Renamed += new System.IO.RenamedEventHandler(this.fileSystemWatcher_Renamed);
this.fileSystemWatcher.Changed += new System.IO.FileSystemEventHandler(this.fileSystemWatcher_Changed);
this.fileSystemWatcher.Created += new System.IO.FileSystemEventHandler(this.fileSystemWatcher_Changed);
//
// textBoxWatch
//
this.textBoxWatch.BackColor = System.Drawing.Color.White;
this.textBoxWatch.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.textBoxWatch.Dock = System.Windows.Forms.DockStyle.Fill;
this.textBoxWatch.Location = new System.Drawing.Point(0, 0);
this.textBoxWatch.Multiline = true;
this.textBoxWatch.Name = "textBoxWatch";
this.textBoxWatch.ReadOnly = true;
this.textBoxWatch.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.textBoxWatch.Size = new System.Drawing.Size(168, 144);
this.textBoxWatch.TabIndex = 0;
this.textBoxWatch.Text = "";
this.textBoxWatch.WordWrap = false;
//
// FtpFileWatcher
//
this.Controls.Add(this.textBoxWatch);
this.Name = "FtpFileWatcher";
this.Size = new System.Drawing.Size(168, 144);
((System.ComponentModel.ISupportInitialize)(this.fileSystemWatcher)).EndInit();
this.ResumeLayout(false);
}
#endregion
public string Watch_Text //属性——监视结果
{
//用return方法返回该属性值
get
{
return textBoxWatch.Text;
}
//用set方法获得该属性值
set
{
textBoxWatch.Text = value;
}
}
public bool Watch_Enable //属性——是否监视
{
get
{
return fileSystemWatcher.EnableRaisingEvents;
}
set
{
fileSystemWatcher.EnableRaisingEvents = value;
}
}
public string Watch_Filter //属性——监视的文件模式类型
{
get
{
return fileSystemWatcher.Filter;
}
set
{
fileSystemWatcher.Filter = value;
}
}
public bool Watch_IncludeSubdirectories //属性——是否包含子目录
{
get
{
return fileSystemWatcher.IncludeSubdirectories;
}
set
{
fileSystemWatcher.IncludeSubdirectories = value;
}
}
public NotifyFilters Watch_NotifyFilter //属性——监视的更改事件的标志
{
get
{
return fileSystemWatcher.NotifyFilter;
}
set
{
fileSystemWatcher.NotifyFilter = value;
}
}
public string Watch_Path //属性——要监视的目录的路径
{
get
{
return fileSystemWatcher.Path;
}
set
{
fileSystemWatcher.Path = value;
}
}
public string Watch_SerIP //属性——要监视的服务器 IP 地址
{
get
{
return SerIP;
}
set
{
SerIP = value;
}
}
public string Watch_Port //属性——要监视的服务器端口
{
get
{
return SerPort;
}
set
{
SerPort = value;
}
}
public string Watch_VFloder //属性——要监视的目录对应服务器的虚拟目录
{
get
{
return VFolder;
}
set
{
VFolder = value;
}
}
public string Str_FindFiles //属性——遍历出的文件结果
{
get
{
return str_found;
}
set
{
str_found = value;
}
}
public string Str_FindDir //属性——正在遍历的目录
{
get
{
return dirs;
}
set
{
dirs = value;
}
}
public int Num_FindFiles //属性——遍历出文件的数目
{
get
{
return num_found_files;
}
set
{
num_found_files = value;
}
}
public bool FindFiles_Enable //属性——遍历状态,true 为正在遍历,False 为遍历完毕,该属性为只读属性
{
get
{
return thread_ergodic.IsAlive;
}
}
public void FindEveryFile(string str_control )//方法——遍历的控制:Find 为开始遍历标示,其他为停止遍历标示
{
if (str_control == "Find")
{
num_found_files = 0;
str_found = "";
dirs = fileSystemWatcher.Path.Trim();
VFolder = VFolder.Trim();
if (VFolder.Length == 0 || VFolder == "/")
{
VFolder = "";
}
else
{
if (VFolder.Substring(0, 1) != "/")
{
VFolder = "/" + VFolder;
}
if (VFolder.Substring(VFolder.Length - 1, 1) == "/")
{
VFolder = VFolder.Remove(VFolder.Length - 1, 1);
}
}
thread_ergodic = new Thread(new ThreadStart(files_ergodic));
thread_ergodic.IsBackground = true;
thread_ergodic.Start();
}
else
{
if (thread_ergodic.IsAlive)
{
try
{
thread_ergodic.Abort();
}
catch
{
thread_ergodic.Join();
}
}
}
}
private void files_ergodic() //文件遍历线程
{
string[] files;
string fTemp;
files = Directory.GetFiles(dirs); //获取当前目录的所有文件名
foreach (string f in files)
{
fTemp = f.Remove(0, fileSystemWatcher.Path.Length);
fTemp = fTemp.Replace("\\", "/");
if (fTemp.Substring(0, 1) != "/")
{
fTemp = "/" + fTemp;
}
if (fTemp.Length < 9 || fTemp.Substring(fTemp.Length - 9, 9) != "Thumbs.db")
{
str_found = fTemp + "\r\n" + str_found;
num_found_files = num_found_files + 1;
}
}
files = Directory.GetDirectories(dirs); //获取当前目录内的子目录名
foreach (string f in files)
{
fTemp = f.Remove(0, fileSystemWatcher.Path.Length);
fTemp = fTemp.Replace("\\", "/");
if (fTemp.Substring(0, 1) != "/")
{
fTemp = "/" + fTemp;
}
if (fTemp != "/System Volume Information" && fTemp != "/Recycled") //去除回收站和系统卷信息文件夹
{
dirs = f;
files_ergodic(); //递归调用
}
Thread.Sleep(0);
}
}
private void fileSystemWatcher_Changed(object sender, System.IO.FileSystemEventArgs e)
{
string strTemp;
strTemp = e.Name + " " + e.ChangeType;
strTemp = "/" + strTemp.Replace("\\", "/");
if (e.Name != "/System Volume Information" && e.Name != "/Recycled")
{
textBoxWatch.Text = strTemp + "\r\n" + textBoxWatch.Text;
}
}
private void fileSystemWatcher_Renamed(object sender, System.IO.RenamedEventArgs e)
{
string strTemp;
strTemp = e.OldName + " " + e.ChangeType + " /" + e.Name;
strTemp = "/" + strTemp.Replace("\\", "/");
textBoxWatch.Text = strTemp + "\r\n" + textBoxWatch.Text;
}
}
}