说明:建立Windows应用程序,在E盘上建立文件夹wang
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace FileWatch
{
public partial class Form1 : Form
{
private FileSystemWatcher watch;
public Form1()
{
InitializeComponent();
DirectoryInfo dir = new DirectoryInfo("E:/wang");
if (!dir.Exists)
{
dir.Create();
}
this.watch = new FileSystemWatcher();
this.watch.Deleted+=new FileSystemEventHandler(this.OnDeleted);
this.watch.Renamed+=new RenamedEventHandler(this.OnRenamed);
this.watch.Changed+=new FileSystemEventHandler(this.OnChanged);
this.watch.Created+=new FileSystemEventHandler(this.OnCreated);
}
public void OnDeleted(Object source,FileSystemEventArgs e)
{
try
{
StreamWriter sw = new StreamWriter("E:/wang/aaa.txt", true);
sw.WriteLine("log: " + System.DateTime.Now + " File: {0} Deleted ", e.FullPath);
sw.Close();
Control.CheckForIllegalCrossThreadCalls = false;//此属性是在.NET Framework 2.0新增加的,如果不设为false,会出错!
label1.Text="Wrote deleted event to log";
}
catch(IOException ex)
{
label1.Text="Error Writting to log"+ex.ToString();
}
}
public void OnRenamed(object source,RenamedEventArgs e)
{
try
{
StreamWriter sw = new StreamWriter("E:/wang/aaa.txt", true);
sw.WriteLine("log: "+System.DateTime.Now +" File Nameed from {0} to {1}",e.OldName ,e.FullPath);
sw.Close();
Control.CheckForIllegalCrossThreadCalls = false;//此属性是在.NET Framework 2.0新增加的,如果不设为false,会出错!
label1.Text = "Wrote Renamed event to log";
}
catch (IOException ex)
{
label1.Text = "Error Writting to log"+ex.ToString();
}
}
public void OnChanged(object source, FileSystemEventArgs e)
{
try
{
StreamWriter sw = new StreamWriter("E:/wang/aaa.txt", true);
sw.WriteLine("log: " + System.DateTime.Now + " File: {0} {1}", e.FullPath, e.ChangeType.ToString());
sw.Close();
Control.CheckForIllegalCrossThreadCalls = false;//此属性是在.NET Framework 2.0新增加的,如果不设为false,会出错!
label1.Text = "Wrote changed event to log";
}
catch (IOException ex)
{
label1.Text = "Error Writting to log"+ex.ToString();
}
}
public void OnCreated(object source, FileSystemEventArgs e)
{
try
{
StreamWriter sw = new StreamWriter("E:/wang/aaa.txt", true);
sw.WriteLine("log: " + System.DateTime.Now + " File {0} Created ", e.FullPath);
sw.Close();
Control.CheckForIllegalCrossThreadCalls = false;//此属性是在.NET Framework 2.0新增加的,如果不设为false,会出错!
label1.Text = "Wrote create event to log";
}
catch (IOException ex)
{
label1.Text = "Error Writting to log"+ex.ToString();
}
}
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() != DialogResult.Cancel)
{
this.textBox1.Text = openFileDialog1.FileName;
}
this.button2.Enabled = true;
}
private void button2_Click(object sender, EventArgs e)
{
watch.Path = Path.GetDirectoryName(this.textBox1.Text);
watch.Filter = Path.GetFileName(this.textBox1.Text);
watch.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.Size;
label1.Text = " Watching: " + this.textBox1.Text;
watch.EnableRaisingEvents = true;
}
}
}
源码下载:http://dl2.youkuaiyun.com/down4/20070910/10202119543.rar