winform监听文件变动(方法搜集整理)
【第一种方式】:
using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Security.Permissions; namespace MyFileSystemWatcher { public class Watcher { public static void Main(string[] args) { Run(); } [PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")] public static void Run() { string[] args = System.Environment.GetCommandLineArgs(); if (args.Length != 2) { Console.WriteLine("使用方式: FileSystemWatcher.exe DirectoryPath"); return; } FileSystemWatcher watcher = new FileSystemWatcher(); watcher.Path = args[1]; /* 设置为监视 LastWrite 和 LastAccess 时间方面的更改,以及目录中文本文件的创建、删除或重命名。 */ watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; // 只监控.txt文件 watcher.Filter = "*.txt"; // 添加事件处理器。 watcher.Changed += new FileSystemEventHandler(OnChanged); watcher.Created += new FileSystemEventHandler(OnChanged); watcher.Deleted += new FileSystemEventHandler(OnChanged); watcher.Renamed += new RenamedEventHandler(OnRenamed); // 开始监控。 watcher.EnableRaisingEvents = true; // 输入q推出程序。 Console.WriteLine("按 \'q\' 推出程序。"); while (Console.Read() != 'q') ; } // 定义事件处理器。 private static void OnChanged(object source, FileSystemEventArgs e) { //如果更改、创建或删除文件,文件路径将被输出到控制台。 Console.WriteLine("文件: " + e.FullPath + " " + e.ChangeType); } private static void OnRenamed(object source, RenamedEventArgs e) { // 在文件重命名后,旧路径和新路径都输出到控制台。 Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath); } } } 使用说明(具体看图): 1、打开cmd窗口,先定位到FileSystemWatcher.exe所在的文件夹目录; 2、输入【FileSystemWatcher.exe 文件夹目录名称】,回车; 3、在监控的文件夹目录中增删改文件,就可以看见监控结果。
【第二种方式】:(已测试)
第一步:窗体拖入fileSystemWatcher控件
第二部:复制下面代码
fileSystemWatcher1.Path = @"C:\Users\ASUS\Desktop";
private void fileSystemWatcher1_Changed(object sender, System.IO.FileSystemEventArgs e)
{
listBox1.Items.Add(string.Format("{0} 内容有改bai变", e.Name));
}
private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
{
listBox1.Items.Add(string .Format ("有新文件du创建zhi,文件名dao{0}", e.Name));
}
private void fileSystemWatcher1_Deleted(object sender, System.IO.FileSystemEventArgs e)
{
listBox1 .Items .Add (string .Format ("<{0}> 被删除",e.Name) );
}
private void fileSystemWatcher1_Renamed(object sender, System.IO.RenamedEventArgs e)
{
listBox1.Items.Add(string.Format("文件{0}被换名为:{1}", e.OldName, e.Name));
}
【第三种方式】:(未测试)
//方法说明:FileSystemWatcher
为每个文件类型创建一个。然后,你可以将它们全部绑定到同一组事件处理程序:
string[] filters = { "*.txt", "*.doc", "*.docx", "*.xls", "*.xlsx" };
List<FileSystemWatcher> watchers = new List<FileSystemWatcher>;
foreach(string f in filters)
{
FileSystemWatcher w = new FileSystemWatcher();
w.Filter = f;
w.Changed = MyChangedHandler;
watchers.Add(w);
}
【第四种方式】:(未测试)
//方法说明:观察所有扩展,然后在OnChange事件中筛选出所需的扩展:
FileSystemWatcher objWatcher = new FileSystemWatcher();
objWatcher.Filter = "*.*";
objWatcher.Changed += new FileSystemEventHandler(OnChanged);
private static void OnChanged(object source, FileSystemEventArgs e)
{
// get the file's extension
string strFileExt = getFileExt(e.FullPath);
// filter file types
if (Regex.IsMatch(strFileExt, @"\.txt)|\.doc", RegexOptions.IgnoreCase))
{
Console.WriteLine("watched file type changed.");
}
}