static void OnFileWatcherDirectoryDeletedEvent(object sender, DirectoryDeletedEventArgs e)
 {
     lock (Lock)
     {
         Console.WriteLine("OnDirectoryDeletedEvent:");
         Console.WriteLine("  FilePath = {0}", e.FilePath);
         Console.WriteLine();
     }
 }
        private void OnFileDeleted(object sender, FileSystemEventArgs e)
        {
            var filePath = e.FullPath;

            //There is no good way of detecting files without extensions and directories with full stops in name. The file/directory has been deleted so we can't find it out from the path.
            var isDirectory = Path.GetExtension(filePath) == string.Empty;

            if (isDirectory)
            {
                lock (_filesRaisingEventsLock)
                {
                    var filesInDirectory = _filesChanging.Keys.Where(x => x.StartsWith(e.FullPath + "/"));

                    foreach (var fileInDirectory in filesInDirectory)
                    {
                        Pop(fileInDirectory);
                    }
                }
                var directoryDeletedEventArgs = new DirectoryDeletedEventArgs(e.FullPath, UserState);
                RaiseAsynchronousOnDirectoryDeletedEvent(directoryDeletedEventArgs);
                return;
            }

            if (!ShouldMonitorFile(filePath)) return;

            lock (_filesRaisingEventsLock)
            {
                if (_filesChanging.ContainsKey(filePath))
                {
                    Pop(filePath);

                    var fileFinishedChangingEventArgs = new FileFinishedChangingEventArgs(filePath, FileEventType.Deleted, UserState);
                    RaiseAsynchronousOnFileFinishedChangingEvent(fileFinishedChangingEventArgs);
                }
            }

            var fileDeletedEventArgs = new FileDeletedEventArgs(e.FullPath, UserState);
            RaiseAsynchronousOnFileDeletedEvent(fileDeletedEventArgs);
        }
 /// <summary>
 /// Template method to add default behaviour for the event
 /// </summary>
 private void OnDirectoryDeletedEvent(DirectoryDeletedEventArgs e)
 {
     // TODO: Implement default behaviour of OnDirectoryDeletedEvent
 }
        /// <summary>
        /// Will raise the event on the current thread synchronously.
        /// i.e. it will wait until all event handlers have processed the event.
        /// </summary>
        /// <param name="e">The state to be passed to the event.</param>
        private void RaiseOnDirectoryDeletedEvent(DirectoryDeletedEventArgs e)
        {
            // Make a temporary copy of the event to avoid possibility of
            // a race condition if the last subscriber unsubscribes
            // immediately after the null check and before the event is raised.

            DirectoryDeletedEventHandler eventHandler;

            if (!Monitor.TryEnter(_directoryDeletedEventLock, _lockTimeout))
            {
                throw new ApplicationException("Timeout waiting for lock - RaiseOnDirectoryDeletedEvent");
            }
            try
            {
                eventHandler = _directoryDeletedEvent;
            }
            finally
            {
                Monitor.Exit(_directoryDeletedEventLock);
            }

            OnDirectoryDeletedEvent(e);

            if (eventHandler != null)
            {
                eventHandler(this, e);
            }
        }
 /// <summary>
 /// Will raise the event on the calling thread synchronously. 
 /// i.e. it will wait until all event handlers have processed the event.
 /// </summary>
 /// <param name="state">The state to be passed to the event.</param>
 private void RaiseCrossThreadOnDirectoryDeletedEvent(DirectoryDeletedEventArgs e)
 {
     _asyncOperation.SynchronizationContext.Send(new SendOrPostCallback(AsynchronousOnDirectoryDeletedEventRaised), e);
 }
 /// <summary>
 /// Will raise the event on the calling thread asynchronously. 
 /// i.e. it will immediatly continue processing even though event 
 /// handlers have not processed the event yet.
 /// </summary>
 /// <param name="state">The state to be passed to the event.</param>
 private void RaiseAsynchronousOnDirectoryDeletedEvent(DirectoryDeletedEventArgs e)
 {
     _asyncOperation.Post(new SendOrPostCallback(AsynchronousOnDirectoryDeletedEventRaised), e);
 }