473,387 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

FileSystemWatcher OnChanged : What Changed ??

TD
I'm watching a folder for new files. When the files arrive, I want to
wait until it is done being written to and then move it.

Every time I move a file into the test folder, it triggers 3 calls to
the OnChanged handler.

I can't tell what is changing between the 2nd and 3rd call to know
whether it is safe to move the file yet? If I was certain it was
always 3, I could go then but I'm not sure that it will always be 3
calls.

I've Console.Writeline'd everything that I can think of but noting
appears to be changing.

Any ideas? I've included the necessary code blocks

** WATCH THE FOLDER ***

FileSystemWatcher watcher = new FileSystemWatcher();

watcher.Path = "c:\\temp\\xml";

watcher.IncludeSubdirectories = false;
watcher.NotifyFilter = NotifyFilters.LastAccess |
NotifyFilters.LastWrite |
NotifyFilters.FileName |
NotifyFilters.DirectoryName |
NotifyFilters.Size;

// Only watch xml files.
watcher.Filter = "*.xml";

watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);

// Begin watching.
watcher.EnableRaisingEvents = true;

// Wait for the user to quit the program.
Console.WriteLine("Press \'q\' to quit the sample.");
while(Console.Read()!='q');


private static void OnChanged(object source, FileSystemEventArgs e)
{

FileInfo fi = new FileInfo(e.FullPath);

if(fi.LastWriteTime >= DateTime.Now)
{
Console.WriteLine("still writing");
}
else
{

Console.WriteLine("done writing");
// MOVE THE FILE
// can't leave like this because of multiple calls
fi.MoveTo("c:\\temp\\xml_to");
}

}

Nov 16 '05 #1
1 5134
Do you have antivirus installed on your system? File system operations might
raise more than one event. (See the Note in the FileSystemWatcher reference
on msdn website,
http://msdn.microsoft.com/library/de...lassTopic.asp).

For your purpose you can try the following workaround:

private static void OnChanged(object sender, FileSystemEventArgs e)
{
FileInfo fi = new FileInfo(e.FullPath);
if(fi.LastWriteTime >= DateTime.Now)
{
Console.WriteLine("still writing");
}
else
{
Console.WriteLine("done writing");
// MOVE THE FILE
// ** Check first whether the file still exists
// ** if exists, move the file
// ** else ignore the operation
if(File.Exists(fi.FullName))
{
fi.MoveTo("c:\\temp\\xml_to");
}
}
}

Hope it helps.

-- Ricky Lee
============================================
^ o^ "When all doors are closed, God will open Windows" ^o^
============================================

"TD" <ho***************@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
I'm watching a folder for new files. When the files arrive, I want to
wait until it is done being written to and then move it.

Every time I move a file into the test folder, it triggers 3 calls to
the OnChanged handler.

I can't tell what is changing between the 2nd and 3rd call to know
whether it is safe to move the file yet? If I was certain it was
always 3, I could go then but I'm not sure that it will always be 3
calls.

I've Console.Writeline'd everything that I can think of but noting
appears to be changing.

Any ideas? I've included the necessary code blocks

** WATCH THE FOLDER ***

FileSystemWatcher watcher = new FileSystemWatcher();

watcher.Path = "c:\\temp\\xml";

watcher.IncludeSubdirectories = false;
watcher.NotifyFilter = NotifyFilters.LastAccess |
NotifyFilters.LastWrite |
NotifyFilters.FileName |
NotifyFilters.DirectoryName |
NotifyFilters.Size;

// Only watch xml files.
watcher.Filter = "*.xml";

watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);

// Begin watching.
watcher.EnableRaisingEvents = true;

// Wait for the user to quit the program.
Console.WriteLine("Press \'q\' to quit the sample.");
while(Console.Read()!='q');


private static void OnChanged(object source, FileSystemEventArgs e)
{

FileInfo fi = new FileInfo(e.FullPath);

if(fi.LastWriteTime >= DateTime.Now)
{
Console.WriteLine("still writing");
}
else
{

Console.WriteLine("done writing");
// MOVE THE FILE
// can't leave like this because of multiple calls
fi.MoveTo("c:\\temp\\xml_to");
}

}


Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Troy Murphy | last post by:
How do I prevent the FileSystemWatcher event to keep firing while the file is being created? When copying a file to the watched folder, the event fires a dozen or more times! Also, the...
0
by: Erik Herje via .NET 247 | last post by:
Hi, I have a Windows Service written in C# whos purpose is to sniff a folder to look for changes in the folder. If a file is created or changed, it should get its name and start an object the...
0
by: zfeld | last post by:
My program has two instances of a control have fileSystemWatchers that are set to monitor the same directory for changes. When they are watching a directory on the local machine, it works fine, two...
0
by: Peter Kirk | last post by:
Hi I am trying to use FileSystemWatcher to watch for changes in a directory. The problem I am exepriencing is a double firing of an OnChanged event when a file is opened, edited, and saved. Why...
1
by: Peter Kirk | last post by:
Hi I am trying to use FileSystemWatcher to watch for changes in a directory. The problem I am exepriencing is a double firing of an OnChanged event when a file is opened, edited, and saved. Why...
0
by: Silver Oak | last post by:
Hi, I'm experimenting with FileSystemWatcher but it doesn't seem like it fires when a new file is created. Everytime I create a new file, it fires Changed event i.e. Case...
0
by: Andreas Reisenhofer | last post by:
Hallo VB.NET's We are trying to use FindFirstChangeNotification under VB6 and FileSystemWatcher with vb.net and got no success on drives mapped with InterDriveClient (5.0,7.11) on a NetApp...
4
by: Andrus | last post by:
To reporoduce, run the code. Observed: Form is shown Expected: message box should displayed. How to fix ? Andrus.
2
by: John Talli | last post by:
System.IO.FileSystemWatcher When a user copies and pastes a file from one directory to a directory that is monitored by the FileSystemWatcher, the FileSystemWatcher executes twice, once for a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.