Using the FileSystemWatcher component to detect when a file is moved
Posted by: Suprotim Agarwal ,
on 8/8/2008,
in
Category WinForms & WinRT
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.
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!
Was this article worth reading? Share it with fellow developers too. Thanks!
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
The Absolutely Awesome jQuery CookBook.
Suprotim received the prestigious Microsoft MVP award for 17 consecutive years, until he resigned from the program in 2025. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.
Get in touch with him on Twitter @suprotimagarwal or at LinkedIn