FileSystemWatcher and unprocessed files
<p>I am using the FileSystemWatcher to notify when the new files gets created in the network directory. We process the text files(about 5KB size) and delete them immediately when the new file gets created in the directory. If the FileSystemWatcher windows service stops for some reason we have to look for the unprocessed files after it gets back up and running. How can I handle if the new file comes while processing the old files from the directory? Any examples please?</p>

<p>Thank you,</p>

<p>Here is the code example I have with simple form.</p>

<pre><code>public partial class Form1 : Form
{
    private System.IO.FileSystemWatcher watcher;
    string tempDirectory = @"C:\test\";
    public Form1()
    {
        InitializeComponent();
        CreateWatcher();
        GetUnprocessedFiles();
    }
private void CreateWatcher()
{
    //Create a new FileSystemWatcher.
    watcher = new FileSystemWatcher();
    watcher.Filter = "*.txt";
    watcher.NotifyFilter = NotifyFilters.FileName;
      //Subscribe to the Created event.
    watcher.Created += new FileSystemEventHandler(watcher_FileCreated);
    watcher.Path = @"C:\test\";
    watcher.EnableRaisingEvents = true;
}

void watcher_FileCreated(object sender, FileSystemEventArgs e)
{
    //Parse text file.
       FileInfo objFileInfo = new FileInfo(e.FullPath);
        if (!objFileInfo.Exists) return;  
        ParseMessage(e.FullPath);
}


  void ParseMessage(string filePath)
  {
       // Parse text file here 
  }

  void GetUnprocessedFiles()
  {
      // Put all txt files into array.
    string[] array1 = Directory.GetFiles(@"C:\test\"); 
    foreach (string name in array1)
    { 
        string path = string.Format("{0}{1}", tempDirectory, name)
        ParseMessage(path);
    }
  }
</code></pre>

<p>}</p>
