C#源码学习之---监控文件系统

说明:建立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.csdn.net/down4/20070910/10202119543.rar

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值