Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using ObjCRuntime;
- using Foundation;
- using CoreFoundation;
- using System.IO;
- namespace LoadRuntimeXAML.iOS
- {
- public class FileWatcher : IDisposable
- {
- public void Dispose()
- {
- StopFileMonitoring();
- }
- public FileWatcher(string filePath) : base(filePath)
- {
- Path = filePath;
- StartMonitoringFile();
- }
- private void StartMonitoringFile()
- {
- fileMonitorSource?.Cancel();
- fileMonitorSource = null;
- var stream = File.OpenRead(Path);
- int fileDescriptor = stream.SafeFileHandle.DangerousGetHandle().ToInt32();
- fileMonitorSource = new DispatchSource.VnodeMonitor(fileDescriptor, VnodeMonitorKind.Delete | VnodeMonitorKind.Extend | VnodeMonitorKind.Write, DispatchQueue.MainQueue);
- fileMonitorSource.SetEventHandler(() =>
- {
- var observedEvents = fileMonitorSource.ObservedEvents;
- Console.WriteLine("Vnode monitor event: {0} for file: {1}", observedEvents, Path);
- if (observedEvents.HasFlag(VnodeMonitorKind.Delete))
- {
- FileDeleted();
- }
- FileChangedEvent?.Invoke(this, Path);
- });
- fileMonitorSource.SetCancelHandler(() =>
- {
- stream.Close();
- });
- fileMonitorSource.Resume();
- }
- private void StopFileMonitoring()
- {
- fileMonitorSource?.Cancel();
- fileMonitorSource = null;
- }
- private void FileDeleted()
- {
- StopFileMonitoring();
- if (File.Exists(Path))
- {
- StartMonitoringFile();
- return;
- }
- else
- {
- // Some editors delete and replace the file on save, instead of actually saving and modifying the file.
- DispatchQueue.MainQueue.DispatchAfter(new DispatchTime(DispatchTime.Now, TimeSpan.FromSeconds(0.5)), () =>
- {
- FileDeleted();
- });
- }
- }
- private DispatchSource.VnodeMonitor fileMonitorSource;
- public string Path { get; }
- public event FileChanged FileChangedEvent;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement