private void DeleteFile(FileSystemWatcherExt FileWatcher, string SourceFilePath)
        {
            string fullDestPath;
            string fileWatcherName = FileWatcher.Path.Replace(@"\", "_").Replace(":", "_");

            try
            {
                // Get the destination path.
                fullDestPath = SourceFilePath.Replace(FileWatcher.Path, FileWatcher.DestinationDirectory +
                                                      "\\" + fileWatcherName).Replace("\\\\", "\\");

                // If the file or directory exists, delete it.
                if (File.Exists(fullDestPath))
                {
                    // Delete the file.
                    File.Delete(fullDestPath);
                }
                else if (Directory.Exists(fullDestPath))
                {
                    // Delete the directory.
                    Directory.Delete(fullDestPath, true);
                }
            }
            catch (Exception ex)
            {
                WriteToLog(new string[] { DateTime.Now.ToString(),
                                          "Error deleting file from " + SourceFilePath + ".", ex.Message });
            }
        }
        private void RenameBackupFile(FileSystemWatcherExt FileWatcher, string OldFile, string NewFile)
        {
            string origPath = "", newPath = "";
            string fileWatcherName = FileWatcher.Path.Replace(@"\", "_").Replace(":", "_");

            try
            {
                // Get the full path of the original backup file.
                origPath = OldFile.Replace(FileWatcher.Path, FileWatcher.DestinationDirectory +
                                           "\\" + fileWatcherName).Replace("\\\\", "\\");

                newPath = NewFile.Replace(FileWatcher.Path, FileWatcher.DestinationDirectory +
                                          "\\" + fileWatcherName).Replace("\\\\", "\\");

                // If it's a directory, rename it.
                if (Directory.Exists(origPath))
                {
                    Directory.Move(origPath, newPath);
                }
                else
                {
                    // If it's a file, rename it.
                    File.Move(origPath, newPath);
                }
            }
            catch (Exception ex)
            {
                WriteToLog(new string[] { DateTime.Now.ToString(),
                                          "Error copying file from " + origPath + " to " + newPath + ".", ex.Message });
            }
        }
        private void fswModel_Renamed(object sender, RenamedEventArgs e)
        {
            FileSystemWatcherExt fswExt = (FileSystemWatcherExt)sender;

            // Delete the backup file and copy the new one.

            RenameBackupFile(fswExt, e.OldFullPath, e.FullPath);
        }
        private void fswModel_Changed(object sender, FileSystemEventArgs e)
        {
            // Get the current file system watcher.
            FileSystemWatcherExt fswExt = (FileSystemWatcherExt)sender;

            // Copy the file
            CopyChangedFile(fswExt, e.FullPath);
        }
        private void fswModel_Created(object sender, FileSystemEventArgs e)
        {
            // Get the current file system watcher and file type object
            // File type object is just used as a demo for now.
            FileSystemWatcherExt fswExt      = (FileSystemWatcherExt)sender;
            FileObject           changedFile = GetAffectedFile(e.FullPath);

            // Copy the file
            CopyChangedFile(fswExt, e.FullPath);
        }
        private void CopyChangedFile(FileSystemWatcherExt FileWatcher, string SourceFilePath)
        {
            string       fullDestPath    = "";
            string       fileWatcherName = FileWatcher.Path.Replace(@"\", "_").Replace(":", "_");
            FileCopySpec fcsCopy         = new FileCopySpec();

            bool allowCopy = true;

            try
            {
                // Get the destination path.
                fullDestPath = SourceFilePath.Replace(FileWatcher.Path, FileWatcher.DestinationDirectory +
                                                      "\\" + fileWatcherName).Replace("\\\\", "\\");

                // If the destination file has been accessed in the last five seconds,
                // Cancel the copy.
                if (FileWatcher.LastFileAccessed == fullDestPath)
                {
                    allowCopy = (FileWatcher.LastOperationTime < DateTime.Now.AddSeconds(-5));
                }

                // Otherwise, go ahead and copy it.
                if (allowCopy)
                {
                    // If it doesn't exist already, create it.
                    if (!Directory.Exists(Directory.GetParent(fullDestPath).ToString()))
                    {
                        Directory.CreateDirectory(Directory.GetParent(fullDestPath).ToString());
                    }

                    // If it's a directory, create the corresponding directory.
                    // Otherwise, just copy the file.
                    if (Directory.Exists(SourceFilePath))
                    {
                        Directory.CreateDirectory(fullDestPath);
                    }
                    else
                    {
                        // Add it to the copy queue
                        fcsCopy.fromLoc = SourceFilePath;
                        fcsCopy.toLoc   = fullDestPath;
                        CopyRequests.Enqueue(fcsCopy);
                    }

                    // Update the filewatcher.
                    FileWatcher.LastFileAccessed  = fullDestPath;
                    FileWatcher.LastOperationTime = DateTime.Now;
                }
            }
            catch (Exception ex)
            {
                WriteToLog(new string[] { DateTime.Now.ToString(),
                                          "Error copying file from " + SourceFilePath + " to " + fullDestPath + ".", ex.Message });
            }
        }
        private FileSystemWatcherExt CreateFileWatcher(string FileSource, bool IncludeSubdirs, string Destination)
        {
            // Create and return a new file system watcher.
            FileSystemWatcherExt fswReturn = new FileSystemWatcherExt(Destination);
            string filePath = "";
            int    charPlace;

            try
            {
                // If the user has included a \ at the end of the source directory.
                if (FileSource.EndsWith(@"\"))
                {
                    FileSource = FileSource.Substring(0, (FileSource.Length - 1));
                }

                // If the string that was passed is an actual directory, use it.
                if (Directory.Exists(FileSource))
                {
                    filePath = FileSource;
                }
                else
                {
                    // Otherwise, parse the string for the directory and the pattern.
                    charPlace = FileSource.LastIndexOf(@"\");
                    filePath  = FileSource.Substring(0, charPlace);

                    // Test again ...
                    if (Directory.Exists(filePath))
                    {
                        fswReturn.Path   = filePath;
                        fswReturn.Filter = FileSource.Substring(charPlace + 1);
                    }
                }

                // Instruct the file watcher to include subdirectories.
                fswReturn.IncludeSubdirectories = IncludeSubdirs;
            }
            catch (Exception ex)
            {
                WriteToLog(new string[] { DateTime.Now.ToString(),
                                          "Error creating FileWatcher on " + FileSource + ".", ex.Message });
            }

            // If the path was valid, return a file watcher ...
            if (fswReturn.Path.Length > 0)
            {
                return(fswReturn);
            }
            else
            {
                // Otherwise, return null.
                return(null);
            }
        }
 private void fswModel_Deleted(object sender, FileSystemEventArgs e)
 {
     FileSystemWatcherExt fswExt = (FileSystemWatcherExt)sender;
 }