Using the FileSystemWatcher component to detect when a file is moved

Posted by: Suprotim Agarwal , on 8/8/2008, in Category WinForms & WinRT
Views: 29590
Abstract: The FileSystemWatcher is a very handy component that notifies and raises events when a directory or a file in a directory changes. The FileSystemWatcher lets you detect several types of changes in a directory or file, however there is no direct way of detecting when a file is 'moved'. That is the subject of this article and we will explore how to do so.
Using the FileSystemWatcher component to detect when a file is moved
 
The FileSystemWatcher is a very handy component that notifies and raises events when a directory or a file(in the directory) changes. The FileSystemWatcher lets you detect several types of changes in a directory or file, like the 'LastWrite' date and time, changes in the size of files or directories etc. It also helps you detect if a file or directory is deleted, renamed or created.
 
However there is no direct way of detecting when a file is moved. That is the subject of this article and we will explore how to do so.
 
When you move a file, two events are raised: Deleted event in the source directory, and a Created event in the destination directory. So in order to detect a file move, we will use 2 FileSystemWatcher components to track them. 
 
Let’s see some code:
 
Step 1: Open Visual Studio > File > New > Project. In the Project Types pane, choose the language of your choice (Visual C# or Visual Basic). In the Templates pane, choose Windows Application. Choose a name and location for the project and click OK.
 
Step 2: Drag and drop 2 FileSystemWatcher components from the ToolBox (Component Tab) on to the form. Rename them to fswSource and fswDestination.
 
Step 3: Now set the ‘Path’ property of the 2 FileSystemWatcher Components. In our case, I have set the path to the following:
 
fswSource - C:\Source
 
fswDestination – D:\Temp
 
We need to detect when a file was copied or moved from C:\Source to D:\Temp. If a file is moved from source to destination, the ‘Created’ event is raised by the 2nd FileSystemWatcher(fswDestination) component. Similarly Deleted event is raised by the 1st FileSystemWatcher(fswSource) component.
 
Step 4: The two events will look similar to the following:
 
C#
 
private void fswSource_Deleted(object sender, System.IO.FileSystemEventArgs e)
{
 
}
 
private void fswDestination_Created(object sender, System.IO.FileSystemEventArgs e)
{
    MessageBox.Show(e.Name + " was moved");
}
 
VB.NET
Private Sub fswSource_Deleted(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs)
 
End Sub
 
Private Sub fswDestination_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs)
      MessageBox.Show(e.Name & " was moved")
End Sub
 
That’s it. Now move or copy a file from C:\Source to D:\Temp. As soon as the file is moved to D:\Temp, the message box fires up indicating the file that has been moved. To find out similar tricks, I would advise you to read more about the FileSystemWatcher over here: http://msdn2.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx. I hope this article was useful and I thank you for viewing it.
If you liked the article,  Subscribe to my RSS Feed.  
 
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+
Further Reading - Articles You May Like!
Author
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry, DNC Magazine for Developers, SQLServerCurry and DevCurry. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls and a new one recently at The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigious Microsoft MVP award for nine times in a row now. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that represents premium web sites and digital publications comprising of Professional web, windows, mobile and cloud developers, technical managers, and architects.

Get in touch with him on Twitter @suprotimagarwal, LinkedIn or befriend him on Facebook



Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by ae on Wednesday, September 17, 2008 5:31 AM
Hi misters,

are there problems using the FileSystemWatcher ??

If I copy or move a big file ( for example, 700 MB), when detect the FileSystemWatcher that change: starting copy or when finish copy ?? Because while the file is copying in disk, I can't not access to file. Using File.Exists( System.IO.FileSystemEventArgs e.Name ) returns false until the file is copied fully.

thanks.
Comment posted by Suprotim Agarwal on Wednesday, September 17, 2008 8:19 AM
The events will fire when the file has been moved
Comment posted by Ross on Monday, December 1, 2008 5:50 PM
I fail to see how this detects a file/directory move. It simply detects a file/directory create. You aren't even using fswSource. Did you missed some code when you created the article?
Comment posted by javier parra on Thursday, July 16, 2009 11:45 AM
How can i watch nultiple direcories like c:\ and D: etc.. ?
Comment posted by Suprotim Agarwal on Friday, July 17, 2009 2:29 AM
Javier: AFAIK, the source can be set to one path. Create Multiple FileSystemWatcher components to track multiple folders 'at the same time'.
Comment posted by netDude on Friday, February 18, 2011 4:06 PM
Is this a joke?
It's not very funny...
Comment posted by Clark on Tuesday, September 20, 2011 1:05 PM
You should change 'MessageBox.Show(e.Name & " was moved")' into 'MessageBox.Show(e.Name & " was created") because your code just detect a new file created. Your article has nothing to do with file move detection!
I suggest you to update or delete this article!
Comment posted by Mihai on Tuesday, July 29, 2014 3:03 PM
Junk...

Categories

JOIN OUR COMMUNITY

POPULAR ARTICLES

FREE .NET MAGAZINES

Free DNC .NET Magazine

Tags

JQUERY COOKBOOK

jQuery CookBook