using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
namespace FileWatch
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox txtLocation;
private System.Windows.Forms.Button cmdBrowse;
private System.Windows.Forms.Button cmdWatch;
private System.Windows.Forms.Label lblWatch;
private System.Windows.Forms.OpenFileDialog FileDialog;
//File System Watcher object
private FileSystemWatcher wathcer;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
DirectoryInfo aDir=new DirectoryInfo("C:\\FileLogs");
if(!aDir.Exists)
aDir.Create();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null) 
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.txtLocation = new System.Windows.Forms.TextBox();
this.cmdBrowse = new System.Windows.Forms.Button();
this.cmdWatch = new System.Windows.Forms.Button();
this.lblWatch = new System.Windows.Forms.Label();
this.FileDialog = new System.Windows.Forms.OpenFileDialog();
this.wathcer = new System.IO.FileSystemWatcher();
((System.ComponentModel.ISupportInitialize)(this.wathcer)).BeginInit();
this.SuspendLayout();
// 
// txtLocation
// 
this.txtLocation.Location = new System.Drawing.Point(8, 24);
this.txtLocation.Name = "txtLocation";
this.txtLocation.Size = new System.Drawing.Size(184, 21);
this.txtLocation.TabIndex = 0;
this.txtLocation.Text = "";
// 
// cmdBrowse
// 
this.cmdBrowse.Location = new System.Drawing.Point(208, 24);
this.cmdBrowse.Name = "cmdBrowse";
this.cmdBrowse.Size = new System.Drawing.Size(64, 24);
this.cmdBrowse.TabIndex = 1;
this.cmdBrowse.Text = "Browse...";
this.cmdBrowse.Click += new System.EventHandler(this.cmdBrowse_Click);
// 
// cmdWatch
// 
this.cmdWatch.Enabled = false;
this.cmdWatch.Location = new System.Drawing.Point(88, 56);
this.cmdWatch.Name = "cmdWatch";
this.cmdWatch.Size = new System.Drawing.Size(80, 32);
this.cmdWatch.TabIndex = 2;
this.cmdWatch.Text = "Watch!";
this.cmdWatch.Click += new System.EventHandler(this.cmdWatch_Click);
// 
// lblWatch
// 
this.lblWatch.Location = new System.Drawing.Point(8, 104);
this.lblWatch.Name = "lblWatch";
this.lblWatch.Size = new System.Drawing.Size(264, 32);
this.lblWatch.TabIndex = 3;
// 
// FileDialog
// 
this.FileDialog.Filter = "All Files|*.*";
// 
// wathcer
// 
this.wathcer=new FileSystemWatcher();
this.wathcer.EnableRaisingEvents=false;

this.wathcer.Deleted += new System.IO.FileSystemEventHandler(this.OnDelete);
this.wathcer.Renamed += new System.IO.RenamedEventHandler(this.OnRenamed);
this.wathcer.Changed += new System.IO.FileSystemEventHandler(this.OnChanged);
this.wathcer.Created += new System.IO.FileSystemEventHandler(this.OnCreate);
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(296, 135);
this.Controls.Add(this.lblWatch);
this.Controls.Add(this.cmdWatch);
this.Controls.Add(this.cmdBrowse);
this.Controls.Add(this.txtLocation);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "FileMonitor";
((System.ComponentModel.ISupportInitialize)(this.wathcer)).EndInit();
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new Form1());
} private void OnDelete(object sender, FileSystemEventArgs e)
{
try
{
StreamWriter sw=new StreamWriter("C:\\FileLogs\\Log.txt",true);
sw.WriteLine("File:{0} Deleted",e.FullPath);
sw.Close();
lblWatch.Text="Wrote delete event to log";
}
catch(IOException ex)
{
lblWatch.Text="Error Writing to log";
}
} private void OnRenamed(object sender, RenamedEventArgs e)
{
try
{
StreamWriter sw=new StreamWriter("C:\\FileLogs\\Log.txt",true);
sw.WriteLine("File renamed from {0} to {1}",e.OldName,e.FullPath);
sw.Close();
lblWatch.Text="Wrote renamed event to log";
}
catch(IOException ex)
{
lblWatch.Text="Error Writing to log";
}
} private void OnChanged(object sender, FileSystemEventArgs e)
{
try
{
StreamWriter sw=new StreamWriter("C:\\FileLogs\\Log.txt",true);
sw.WriteLine("File:{0} {1}",e.FullPath,e.ChangeType.ToString());
sw.Close();
lblWatch.Text="Wrote change event to log";
}
catch(IOException ex)
{
lblWatch.Text="Error Writing to log";
}
} private void OnCreate(object sender, FileSystemEventArgs e)
{
try
{
StreamWriter sw=new StreamWriter("C:\\FileLogs\\Log.txt",true);
sw.WriteLine("File:{0} Created",e.FullPath);
sw.Close();
lblWatch.Text="Wrote Create event to log";
}
catch(IOException ex)
{
lblWatch.Text="Error Writing to log";
}
} private void cmdBrowse_Click(object sender, System.EventArgs e)
{
if(FileDialog.ShowDialog()!=DialogResult.Cancel)
{
this.txtLocation.Text=FileDialog.FileName;
}
cmdWatch.Enabled=true;
} private void cmdWatch_Click(object sender, System.EventArgs e)
{
this.wathcer.Path=Path.GetFullPath(this.txtLocation.Text);
this.wathcer.Filter=Path.GetFileName(this.txtLocation.Text);
this.wathcer.NotifyFilter=NotifyFilters.LastWrite|NotifyFilters.FileName|NotifyFilters.Size;
lblWatch.Text="Watching "+txtLocation.Text;
//Begin watching.
wathcer.EnableRaisingEvents=true;
}
}
}最后在this.wathcer.Filter=Path.GetFileName(this.txtLocation.Text);出现异常 
这句我后来新建个项目却可以通过 这里就不行了
System.ArgumentException: 目录名 C:\xixi.txt 无效。