FileSystemWatcher monitor newly created file

G

Guest

I'm working with the FileSystemWatcher which has a Created event. But this
event is raised as soon as the new file begins to be written on disk. What I
want is a notification after a file being entirely written. Is there anyway
to achieve this?

Thanks.

Daniel
 
L

Linda Liu [MSFT]

Hi Daniel,

I performed a test based on your description and saw the same thing as you
did.

In my test, I watch all .txt files in a specified folder and set the
NotifyFilter property of the FileSystemWatcher as follows:

watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite |
NotifyFilters.LastAccess ;

Run the application. Open the Windows Explorer and navigate to the
specified folder. As soon as I create a new txt file in the folder, the
Created event of the FileSystemWatcher is fired. Then I rename the new
file's name, and the Renamed event of the FileSystemWatcher is raised.

Press the Enter key and the new txt file is opened in Notepad. Type some
textes in the file and save the changes. At this time, the Changed event of
the FileSystemWatcher is fired.

I suggest that you use the Changed event of the FileSystemWatcher component
to get notified after a file being entirely written on disk.

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
L

LaptopHeaven

I have run into this problem before too. The problem I find with
Linda's suggestion is when you have a very large file being written
(at least a couple megs), the Changed event is fired multiple times,
each time the buffer is written to disk. Here's the process I used to
overcome this problem. It's by no means the best, but it works.

When the Created event fires, I add the FileSystemEventArgs to a
Queue.
Then I have a Timer which runs every XX seconds to try and process the
1st item in the queue.
If the processing fails, I add the item back onto the Queue.



using System;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Xml;

namespace kygeek.Examples
{
class Program
{
private static Queue<FileSystemEventArgs> m_FileQueue = new
Queue<FileSystemEventArgs>();

static void Main(string[] args)
{
Console.Title = "kygeek.Example.FileSystemWatcher";
string sourceDirectory =
ConfigurationManager.AppSettings["SourceDirectory"];
string fileFilter = ConfigurationManager.AppSettings["FileFilter"];

FileSystemWatcher incoming = new FileSystemWatcher();
incoming.Path = sourceDirectory;
incoming.NotifyFilter = NotifyFilters.LastAccess |
NotifyFilters.LastWrite | NotifyFilters.FileName |
NotifyFilters.DirectoryName;
incoming.Filter = fileFilter;
incoming.Created += new
FileSystemEventHandler(OnCreatedHandler);
incoming.EnableRaisingEvents = true;

StartTimerThread();

Console.WriteLine("Press \'q\' to quit.");
while (Console.Read() != 'q') ;
}

static void OnCreatedHandler(object sender,
FileSystemEventArgs e)
{
Console.WriteLine();
Console.WriteLine("File {0} created.", e.Name);
m_FileQueue.Enqueue(e);
}

private static void StartTimer()
{
System.Timers.Timer timer = new System.Timers.Timer();
timer.Elapsed += new
System.Timers.ElapsedEventHandler(OnTimerElapsed);
timer.Interval = 10000;
timer.Enabled = true;
GC.KeepAlive(timer);

}

static void OnTimerElapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
if (m_FileQueue.Count > 0)
{
FileSystemEventArgs fileSysEventArg =
m_FileQueue.Dequeue();
try
{
ProcessFile(fileSysEventArg);
}
catch (Exception ex)
{
m_FileQueue.Enqueue(fileSysEventArg);
}
}
}
private static void StartTimerThread()
{
System.Threading.ThreadStart timerThreadStart = new
System.Threading.ThreadStart(StartTimer);
System.Threading.Thread timerThread = new
System.Threading.Thread(timerThreadStart);
timerThread.Start();
}

private static void ProcessFile(FileSystemEventArgs e)
{
Console.WriteLine("Trying to do something to the
file....");
}
}
}
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Daniel said:
I'm working with the FileSystemWatcher which has a Created event. But this
event is raised as soon as the new file begins to be written on disk. What
I
want is a notification after a file being entirely written. Is there
anyway
to achieve this?

The FileSystemWatcher is sometimes a pain to work with. For reasons like the
one you are encountering. In short the answer to your question is NO. You
can still do it but you have to use different techniques depending of your
escenario.
If you cnotrol the file creation (maybe it's generated by anothe rprocess)
the easiest is to create a second file (a "flag" file) and has the
FileSystemWatcher check fr it, when you see it you know that there is
another file with the same name that contains the data.

If this is not a possible solution then you will have to do a try/error
until you can read the file, additionally you have to be carefull not to
process the file twice. !!!
 
L

Linda Liu [MSFT]

Hi Daniel,

How about the problem now?

If you still need our further assistance, please feel free to let me know.

Thank you for using our MSDN Managed Newsgroup Support Service!

Sincerely,
Linda Liu
Microsoft Online Community Support
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top