C#中使用FileSystemWatcher检测文件变化

C#中使用FileSystemWatcher检测文件变化
原文地址: http://seanli888.blog.51cto.com/345958/112276
.
Net提供了FileSystemWatcher类用于实现文件监控功能。
FileSystemWatcher位于System.IO名称空间下,使用前需using System.IO;
 
FileSystemWatcher可以监控指定目录下的文件删除,创建,重命名等操作。在其构造函数中可以指定需要监控的目录以及需要监控的特定文件类型。
其Created事件在指定目录下创建文件的时候触发。
然而在实际应用中我们常常需要在需要监控的目录中文件创建完成时才作出相应的处理,而这个事件是在有文件创建的时候就触发的,这在处理大文件的时候就容易出错,因为文件还没创建完成,比如复制一个大文件。这时候我们需要对创建的文件进行访问的时候就会出现无法打开文件的异常。
很多网友都是通过循环检查创建的文件大小变化来判断文件是否完成的,这样带来了很大的系统性能损耗,而且不稳定。
其实我们可以使用一个变通的办法,那就是在创建完大文件的时候创建一个同名的特定类型的小文件,前面我们已经说到FileSystemWatcher类是可以指定监控特定类型的文件的,所以我们就可以安全的处理创建的文件了。
FileSystemWatcher fsw = new FileSystemWatcher(@"D:\aaa");
fsw.Created += new FileSystemEventHandler(fsw_Created);


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 FileWatch : Form
    
{
        
全局变量#region 全局变量
        
private FileSystemWatcher mWatch = null;
        
#endregion


        
窗体事件#region 窗体事件
        
public FileWatch()
        
{
            InitializeComponent();

            
this.mWatch = new FileSystemWatcher();
            
this.mWatch.Changed += new FileSystemEventHandler(mWatch_Changed);
            
this.mWatch.Created += new FileSystemEventHandler(mWatch_Created);
            
this.mWatch.Deleted += new FileSystemEventHandler(mWatch_Deleted);
        }

        
private void btnSelectPath_Click(object sender, EventArgs e)
        
{
            FolderBrowserDialog dlg 
= new FolderBrowserDialog();
            
if (dlg.ShowDialog() == DialogResult.OK)
            
{
                
this.mWatch.Path = dlg.SelectedPath;
                
this.mWatch.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.DirectoryName | NotifyFilters.FileName;
                
this.textBoxPath.Text = dlg.SelectedPath;
                
this.mWatch.EnableRaisingEvents = true;
            }

        }

        
#endregion


        
变化UI#region 变化UI
        
private delegate void ShowInfoHandler(String str);
        
private void ShowInfo(string strInfo)
        
{
            
if (this.InvokeRequired)
            
{
                
this.Invoke(new ShowInfoHandler(this.ShowInfo), new object[] { strInfo });
            }

            
else
            
{
                
this.rtbHistory.AppendText(strInfo);
            }

        }

        
#endregion


        
监控事件#region 监控事件
        
void mWatch_Deleted(object sender, FileSystemEventArgs e)
        
{
            ShowInfo(e.ChangeType.ToString() 
+ "\t" + e.Name + "\r\n");
        }


        
void mWatch_Created(object sender, FileSystemEventArgs e)
        
{
            ShowInfo(e.ChangeType.ToString() 
+ "\t" + e.Name + "\r\n");
        }


        
void mWatch_Changed(object sender, FileSystemEventArgs e)
        
{
            ShowInfo(e.ChangeType.ToString() 
+ "\t" + e.Name + "\r\n");
        }

        
#endregion

        
    }

}

你可能感兴趣的:(C#中使用FileSystemWatcher检测文件变化)