Search code examples
c#xamarin.formsfilesystemwatchernotimplementedexception

Xamarin Forms FileSystemWatcher


i need to watch a folder that are inside my ios/android application.

I would like to use FileSystemWatcher.

This is my code:

var DocFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
var InboxFolder = DocFolder + "/Inbox";

 FileSystemWatcher watcher = new FileSystemWatcher() {
     Path = InboxFolder,
     NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName,
     Filter = "*.*",
     EnableRaisingEvents = true
 };

watcher.Created += (object sender, FileSystemEventArgs e) => {
    // do stuff
};

When the FileSystemWatcher is initializated this error is triggered:

System.NotImplementedException: The method or operation is not implemented.

How can I make this works?

There is another way to watch a folder?

Thank you!


Solution

  • FileSystemWatcher is a .NET Framework-based class which is currently not implemented in Xamarin, although the API is available in .NET Standard 2.0.

    In UWP you can create a StorageFolderQuery with ContentChanged event handler to observe changes in a given folder. See documentation here.

    Android has it's own alternative implementation in the form of Android.OS.FileObserver.

    iOS does not seem to have anything out of the box, so this will probably either require you to find a library that does this job for you or to implement such functionality yourself, albeit in a less efficient manner than if it were built-in to the SDK.