There is a FileSystemWatcher
in our WPF application that monitors a directory and shows a live view of the files in this directory. Each time the folder changes (a file is created, renamed, deleted or changed), the FileSystemWatcher
refreshes this live view.
We have noticed the FileSystemWatcher
events are not triggered when a JPG file is rotated by using the rotate buttons in Windows Explorer (it does work with PNG, BMP, TIFF and TIF files):
Doubleclicking the file and rotating the JPG in the photo app does trigger the events, so there must be a difference here.
I have only been able to test it on Windows 10 but my coworker confirms it worked in previous Windows versions.
Is there a way to detect a JPG file rotation using the FileSystemWatcher
or is this a known bug?
The issue can be tested by using the following code:
public MainWindow()
{
InitializeComponent();
var watcher = new System.IO.FileSystemWatcher();
watcher.Path = Environment.GetFolderPath(System.Environment.SpecialFolder.MyPictures);
watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.Size;
watcher.Filter = "*.*";
watcher.IncludeSubdirectories = true;
watcher.EnableRaisingEvents = true;
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Changed += new FileSystemEventHandler(OnChanged);
}
private void OnChanged(object sender, FileSystemEventArgs e)
{
Debug.WriteLine("The file " + e.Name + " was " + e.ChangeType);
}
That's most likely because in JPEG rotation is usually controlled with EXIF metadata flag. So when you rotate jpeg in explorer - no actual rotation is performed. Instead it just changes EXIF Rotation value (which can be 0,1,2,3 etc as I remember). When you open file in some viewer - it reads rotation flag and performs rotation for display. Because flag has fixed size (takes fixed number of bytes in file) - this operation does not change size of the file, and you only track size changes. To fix - add NotifyFilters.LastWrite
to your FileSystemWatcher
filter. There are other operations that do not change size of a file, so doing this is beneficial anyway.