使用控制线程监视目录文件变化

本文介绍了一个使用C#实现的文件系统监控程序。该程序通过FileSystemWatcher组件监听指定目录下的文件变化(如创建、删除、修改和重命名),并在检测到变化时记录相关信息。程序还采用了线程安全的设计来确保在多线程环境下正确更新UI。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
using System;
using System.IO;
using System.Xml;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;
using SynchroManager;
using SynchroManager.baseclass;



namespace SynchroManager
{
    
public partial class frmChange : Form
    
{
        
public frmChange()
        
{
            InitializeComponent();
         }

        
delegate void SetTextCallback(string text);

        
private string s;
        
private string _dowlist;
        
private string _LookFolder;
        
private Thread demoThread = null;

        
private void btnRun_Click(object sender, EventArgs e)
        
{
            FileSystemWatcher watcher 
= new FileSystemWatcher();
            
if (txtFilter.Text.Length < 2)
            
{
                MessageBox.Show(
"请选择监视目录");
                
return;
            }


            watcher.Path 
= txtFilter.Text;
            watcher.NotifyFilter 
= NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;

            watcher.Filter 
= "";


            watcher.Changed 
+= new FileSystemEventHandler(OnChanged);
            watcher.Created 
+= new FileSystemEventHandler(OnCreated);
            watcher.Deleted 
+= new FileSystemEventHandler(OnDeleted);
            watcher.Renamed 
+= new RenamedEventHandler(OnRenamed);
            watcher.EnableRaisingEvents 
= true;
            watcher.EndInit();

        }


        
private void OnChanged(object source, FileSystemEventArgs e)
        
{
            s 
= e.FullPath + " " + e.ChangeType;
            
this.demoThread = new Thread(new ThreadStart(this.ThreadProcSafe));
            
this.demoThread.Start();
            
//StreamWriter  strW = File.CreateText("C:/Inetpub/ftproot/downlist.txt");
            
//strW.WriteLine(s);
            
//strW.Close();


        }

        
private void OnCreated(object source, FileSystemEventArgs e)
        
{
            s 
= e.FullPath + " " + e.ChangeType;
            
this.demoThread = new Thread(new ThreadStart(this.ThreadProcSafe));
            
this.demoThread.Start();
            
//StreamWriter strW = File.CreateText("C:/Inetpub/ftproot/downlist.txt");
            
//strW.WriteLine(s);
            
//strW.Close();

        }

        
private void OnDeleted(object source, FileSystemEventArgs e)
        
{
            s 
= e.FullPath + " " + e.ChangeType;
            
this.demoThread = new Thread(new ThreadStart(this.ThreadProcSafe));
            
this.demoThread.Start();
            
//StreamWriter strW = File.CreateText("C:/Inetpub/ftproot/downlist.txt");
            
//strW.WriteLine(s);
            
//strW.Close();

        }


        
private void OnRenamed(object source, FileSystemEventArgs e)
        
{
            s 
= e.FullPath + " " + e.ChangeType;
            
this.demoThread = new Thread(new ThreadStart(this.ThreadProcSafe));
            
this.demoThread.Start();

            
//StreamWriter strW = File.CreateText("C:/Inetpub/ftproot/downlist.txt");
            
//while((st=strR.ReadLine())!=null;
            
//strW.WriteLine(s);
            
//strW.Close();

        }


        private void SetText(string text)
        {
            if (this.lblist.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                this.lblist.Items.Add(text);
            }
        }
        private void ThreadProcSafe()
        {
            this.SetText(s);
        }

      }

}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值