Example #1
0
 static void OnFileWatcherDirectoryRenamedEvent(object sender, DirectoryRenamedEventArgs e)
 {
     lock (Lock)
     {
         Console.WriteLine("OnDirectoryRenamedEvent:");
         Console.WriteLine("  OldFilePath = {0}", e.OldFilePath);
         Console.WriteLine("  NewFilePath = {0}", e.NewFilePath);
         Console.WriteLine();
     }
 }
 /// <summary>
 /// Template method to add default behaviour for the event
 /// </summary>
 private void OnDirectoryRenamedEvent(DirectoryRenamedEventArgs e)
 {
     // TODO: Implement default behaviour of OnDirectoryRenamedEvent
 }
        /// <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 RaiseOnDirectoryRenamedEvent(DirectoryRenamedEventArgs 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.

            DirectoryRenamedEventHandler eventHandler;

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

            OnDirectoryRenamedEvent(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 RaiseCrossThreadOnDirectoryRenamedEvent(DirectoryRenamedEventArgs e)
 {
     _asyncOperation.SynchronizationContext.Send(new SendOrPostCallback(AsynchronousOnDirectoryRenamedEventRaised), 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 RaiseAsynchronousOnDirectoryRenamedEvent(DirectoryRenamedEventArgs e)
 {
     _asyncOperation.Post(new SendOrPostCallback(AsynchronousOnDirectoryRenamedEventRaised), e);
 }
        private void OnFileRenamed(object sender, RenamedEventArgs e)
        {
            var filePath = e.FullPath;

            var isDirectory = Directory.Exists(filePath) && (File.GetAttributes(filePath) & FileAttributes.Directory) == FileAttributes.Directory;

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

                    foreach (var fileInDirectory in filesInDirectory)
                    {
                        var fileInDirectoryPath = _filesChanging[fileInDirectory].FilePath;
                        Pop(fileInDirectory);

                        var fileFinishedChangingEventArgs = new FileFinishedChangingEventArgs(fileInDirectoryPath, FileEventType.Renamed, UserState);
                        RaiseAsynchronousOnFileFinishedChangingEvent(fileFinishedChangingEventArgs);
                    }
                }

                var directoryRenamedEventArgs = new DirectoryRenamedEventArgs(e.OldFullPath, e.FullPath, UserState);
                RaiseAsynchronousOnDirectoryRenamedEvent(directoryRenamedEventArgs);
                return;
            }

            if (!ShouldMonitorFile(filePath)) return;

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

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

            var fileRenamedEventArgs = new FileRenamedEventArgs(e.OldFullPath, e.FullPath, UserState);
            RaiseAsynchronousOnFileRenamedEvent(fileRenamedEventArgs);
        }