许多情况下,如果创建或修改一个文件,系统需要执行某些任务。例如,在一个数据库中插入平面文件的重要系统就是如此。

在这种情况下,程序必须监控目录中新建立的文件。如果目录中新建了一个文件,程序必须提取这个文件、进行解析、再把它插入到数据库中。

许多时候,这类功能要通过“调查”目录并列举上次调查后任何新建的文件来完成。在.NET Framework中,微软为开发者提供另一种连续调查一个目录中的新建文件的方法——FileSystemWatcher对象。

FileSystemWatcher对象为你完成目录监控工作。如果新建、更新或删除一个文件,FileSystemWatcher将提交一个事件,通知你发生了一项改变。这样,新建一个文件后,你的程序立即就知道可以使用这个文件。立即通知改变使得你的系统以更高的效率工作,因为你不能总是“调查”目录中发生的改变,而且在两次目录扫描之间也不会有时间流失。

FileSystemWatcher基础

在应用FileSystemWatcher对象之前,你必须了解这个对象的一些基本属性和事件。毫无疑问,这个对象的最重要的属性为“EnableRaisingEvents”属性。

这个属性决定对象在收到改变通知时是否提交事件。如果EnableRaisingEvents属性设为假,对象将不会提交改变事件。如果设为真,它将提交改变事件。下面是你在应用FileSystemWatcher对象时将要用到的其它一些重要属性/事件:

属性:

  • Path——这个属性告诉FileSystemWatcher它需要监控哪条路径。例如,如果我们将这个属性设为“C:Temp”,对象就监控那个目录发生的所有改变。
  • IncludeSubDirectories——这个属性说明FileSystemWatcher对象是否应该监控子目录中发生的改变。
  • Filter——这个属性允许你过滤掉某些类型的文件发生的变化。例如,如果我们只希望在TXT文件被修改/新建/删除时提交通知,可以将这个属性设为“*txt”。在处理高流量或大型目录时,使用这个属性非常方便。

事件

  • Changed——当被监控的目录中有一个文件被修改时,就提交这个事件。值得注意的是,这个事件可能会被提交多次,即使文件的内容仅仅发生一项改变。这是由于在保存文件时,文件的其它属性也发生了改变。
  • Created——当被监控的目录新建一个文件时,就提交这个事件。如果你计划用这个事件移动新建的事件,你必须在事件处理器中写入一些错误处理代码,它能处理当前文件被其它进程使用的情况。之所以要这样做,是因为Created事件可能在建立文件的进程释放文件之前就被提交。如果你没有准备正确处理这种情况的代码,就可能出现异常。
  • Deleted——当被监控的目录中有一个文件被删除,就提交这个事件。
  • Renamed——当被监控的目录中有一个文件被重命名,就提交这个事件。

:如果你没有将EnableRaisingEvents设为真,系统不会提交任何一个事件。如果有时FileSystemWatcher对象似乎无法工作,请首先检查EnableRaisingEvents,确保它被设为真。

事件处理

当FileSystemWatcher调用一个事件处理器时,它包含两个自变量——一个叫做“sender”的对象和一个叫做“e”的FileSystemEventArgs对象。我们感兴趣的自变量为FileSystemEventArgs自变量。这个对象中包含有提交事件的原因。以下是FileSystemEventArgs对象的一些属性:

属性:

  • Name——这个属性中使事件被提交的文件的名称。其中并不包含文件的路径——只包含使用事件被提交的文件或目录名称。
  • ChangeType——这是一个WatcherChangeTypes,它指出要提交哪个类型的事件。其有效值包括:
    Changed
    Created
    Deleted
    Renamed
  • FullPath——这个属性中包含使事件被提交的文件的完整路径,包括文件名和目录名。

代码实例

列表A列出了FileSystemWatcher对象的一个简单应用实例。在这个例子中,我们监控“C:Temp”目录中是否建立了*.TXT文件。这可能是FileSystemWatcher的最简单用法。

列表A

publicvoid CreateWatcher()

{

//Create a new FileSystemWatcher.

FileSystemWatcher watcher = newFileSystemWatcher();

//Set the filter to only catch TXT files.

watcher.Filter = "*.txt";

//Subscribe to the Created event.

watcher.Created += new

FileSystemEventHandler(watcher_FileCreated);

//Set the path to C:Temp

watcher.Path = @"C:Temp";

//Enable the FileSystemWatcher events.

watcher.EnableRaisingEvents = true;

}

void watcher_FileCreated(object sender, FileSystemEventArgs e)

{

//A new .TXT file has been created in C:Temp

Console.WriteLine("A new *.txt file has been created!");

}

这篇文章下载版本的样本项目中还提供了另外一些实例。

应用

最初,FileSystemWatcher的用途可能并不明显。当然,如果一个文件或目录发生改变,它会向我们发出警报,但在哪些情况下我们需要这类信息呢?

如果一个系统允许客户端上传文件进行处理,这时可能就要用到FileSystemWatcher对象。尽管很明显我们已经进入Web服务和SOA时代,但许多系统仍然要将平面文件插入到某些位置,不管是FTP服务器或网络共享文件夹中。我已经将FileSystemWatcher对象应用到这种类型的系统中,虽然并不是完全没有出错,但它在“调查”目录变化方面确实有一定的优势。

如果你想让两个文件保持同步,这时也可以用到FileSystemWatcher对象。你可以用FileSystemWatcher同时监控这两个文件的位置,如果其中一个出现更新,就提交一个Changed事件;然后你就可以将这些改变复制到其它系统/文件中。

如果你的系统有任何功能需要进行文件或目录监控,你必须仔细了解那个组件,看是否可以使用FileSystemWatcher对象。在我的应用过程中,这个对象至少为送交给系统的每个文件节省了10秒钟的时间。

 

C#中利用FileSystemWatcher对单个文件内容的监视

在项目中需要用FileSystemWatcher对单个文件内容进行监视,文件中每添加一行则需要Fire相应的事件,代码如下

   public class CommandWatcher
    
{


        
public event EventHandler<CommandEventArgs> CommandHandler;
      

        
Fields Fields


        
public CommandWatcher(string cmdfile)
        
{
            
this.fileName = cmdfile;
            isChangeImmediate = 
true;

         
        }



        
        
//
        // Summary:
        //     Raises the System.IO.FileSystemWatcher.Changed event.
        //
        // Parameters:
        //   e:
        //     A System.IO.FileSystemEventArgs that contains the event data.
        protected void OnChanged(object sender, FileSystemEventArgs e)   
        
{
            ReloadFile();
        }


    
        
//
        // Summary:
        //     Raises the System.IO.FileSystemWatcher.Error event.
        //
        // Parameters:
        //   e:
        //     An System.IO.ErrorEventArgs that contains the event data.
        protected void OnError(object sender, ErrorEventArgs e)
        
{

        }

        
//
        // Summary:
        //     Raises the System.IO.FileSystemWatcher.Renamed event.
        //
        // Parameters:
        //   e:
        //     A System.IO.RenamedEventArgs that contains the event data.
        protected void OnRenamed(object sender, RenamedEventArgs e)
        
{

        }



        
/// <summary>
        
/// Hook up for the currently selected monitoring method.
        
/// </summary>
        public void Start()
        
{
            
if (isChangeImmediate)
            
{
                
// stop timer if running
                ClearTimer();
                
// If a file is set and the watcher has not been set up yet then create it
                if (null != fileName && null == watcher)
                
{
                    
string path = Path.GetDirectoryName(fileName);
                    
string baseName = Path.GetFileName(fileName);
                    
// Use the FileSystemWatcher class to detect changes to the log file immediately
                    // We must watch for Changed, Created, Deleted, and Renamed events to cover all cases
                    watcher = new System.IO.FileSystemWatcher(path, baseName);
                    FileSystemEventHandler handler = 
new FileSystemEventHandler(OnChanged);
                    watcher.Changed += handler;
                    watcher.Created += handler;
                    watcher.Deleted += handler;
                    watcher.Renamed += OnRenamed;
                    
// Without setting EnableRaisingEvents nothing happens
                    watcher.EnableRaisingEvents = true;
                }
            }
            
else
            
{
                
// On a timer so clear the watcher if previously set up
                ClearWatcher();
                
int numSeconds = 5;
               
                
if (null != timer)
                
{
                    
// Timer is already running so just make sure the interval is correct
                    if (timer.Interval != 1000 * numSeconds)
                    
{
                        timer.Interval = 1000 * numSeconds;
                    }
                }
                
else
                
{
                    
// Fire up a timer if the user entered a valid time interval
                    if (0 != numSeconds)
                    
{
                        timer = 
new System.Windows.Forms.Timer();
                        timer.Interval = 1000 * numSeconds;
                        timer.Tick += timerTick;
                        timer.Start();
                    }
                }
            }
            
this.ReloadFile();
        }

        
void timerTick(object sender, EventArgs e)
        
{
            UpdateBasedOnFileTime();
        }


        
/// <summary>
        
/// Update the log file if the modified timestamp has changed since the last time the file was read.
        
/// </summary>
        private void UpdateBasedOnFileTime()
        
{
            
if (null != fileName)
            
{
                
// This returns "12:00 midnight, January 1, 1601 A.D." if the file does not exist
                DateTime newLastModifiedTime = File.GetLastWriteTime(fileName);
                
if ((newLastModifiedTime.Year < 1620 && newLastModifiedTime != lastModifiedTime)
                    || newLastModifiedTime > lastModifiedTime)
                
{
                    
//OnLogFileChanged();
                    ReloadFile();
                }
            }
        }
        
/// <summary>
        
/// Read the log file.
        
/// </summary>
        
/// <remarks>If the user has some text selected then keep that text selected.</remarks>
        private void ReloadFile()
        
{
            
if (reloadingFile) return// already busy doing this
            reloadingFile = true;
            
try
            
{
                
if (null == fileName)
                
{
                   
// textBoxContents.Text = "";
                }
                
else
                
{
                    
string newFileLines = "";
                    lastModifiedTime = File.GetLastWriteTime(fileName); 
// Remember when we last read in this file
                    long newLength = 0;  // will be set properly later if all is well
                    bool fileExists = File.Exists(fileName);
                    
bool needToClear = !fileExists;
                    
if (fileExists)
                    
{
                        
// It is possible the file will be in use when we read it (especially if using Immediate mode)
                        // So, we will try up to 5 times to access the file
                        int count = 0;
                        
bool success = false;
                        
while (count < 5 && !success)
                        
{
                            
try
                            
{
                                
// Open with the least amount of locking possible
                                using (FileStream stream = File.Open(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
                                
{
                                    newLength = stream.Length;
                                    
if (newLength >= lastFileSize)
                                    
{
                                        stream.Position = lastFileSize;  
// Only read in new lines added
                                    }
                                    
else
                                    
{
                                        needToClear = 
true;
                                    }
                                    
using (StreamReader reader = new StreamReader(stream))
                                    
{
                                        newFileLines = reader.ReadToEnd();

                                        
if (newFileLines != string.Empty)
                                        
{
                                            
if (this.CommandHandler != null)
                                                
this.CommandHandler(thisnew CommandEventArgs(newFileLines));
                                        }
                                      
                                        
//string []tmp = newFileLines.Split(seprater, StringSplitOptions.RemoveEmptyEntries);

                                    }
                                }
                                success = 
true;
                            }
                            
catch (IOException)
                            
{
                                System.Threading.Thread.Sleep(50); 
// Give a little while to free up file
                            }
                            ++count;
                        }
                    }
                    
// Remember the current file length so we can read only newly added log lines the next time the log file is read
                    lastFileSize = newLength;
                    
if(lastFileSize > 1024 * 1024 * 2)
                    
{
                        File.Delete(fileName);
                        File.Create(fileName);
                    }                   
               
                    
if (0 != newFileLines.Length)
                    
{
                        
// See if this log file has proper cr/lf and if not convert
                        int lastCr = newFileLines.LastIndexOf('\n');
                        
if (-1 != lastCr && 0 < lastCr)
                        
{
                            
if (newFileLines[lastCr - 1] != '\r')
                            
{
                                
// OK, this file only has Cr and we need to convert to CrLf
                                newFileLines = newFileLines.Replace("\n", "\r\n");
                            }
                        }
                    }
                    
                   
                }
            }
            
finally
            
{
                
// Put clearing this flag in a finally so we know it will be cleared
                reloadingFile = false;
            }
        }


        
/// <summary>
        
/// Cleanup the <see cref="Timer"/>
        
/// </summary>
        private void ClearTimer()
        
{
            
if (null != timer)
            
{
                timer.Tick -= timerTick;
                timer.Dispose();
                timer = 
null;
            }
        }

        
/// <summary>
        
/// Cleanup the <see cref="FileSystemWatcher"/>
        
/// </summary>
        private void ClearWatcher()
        
{
            
if (null != watcher)
            
{
                FileSystemEventHandler handler = 
new FileSystemEventHandler(OnChanged);
                watcher.Changed -= handler;
                watcher.Created -= handler;
                watcher.Deleted -= handler;
                watcher.Renamed -= OnRenamed;
                watcher.Dispose();
                watcher = 
null;
            }
        }