コード例 #1
0
        private void DeleteFile(FileSystemWatcherExtClass FileWatcher, string sourceFilePath)
        {
            string destinationPath;

            try
            {
                //Get the destination path
                destinationPath = sourceFilePath.Replace(FileWatcher.Path, FileWatcher.DestinationDirectory)
                                  .Replace("\\\\", "\\");

                //If the file or directory exists, delete it
                if (File.Exists(destinationPath))
                {
                    File.Delete(destinationPath);
                }
                else if (Directory.Exists(destinationPath))
                {
                    Directory.Delete(destinationPath, true);
                }
            }
            catch (Exception ex)
            {
                LogAction(new string[] { DateTime.Now.ToString() + " Error Deleting file from " +
                                         sourceFilePath + ". " + ex.Message });
            }
        }
コード例 #2
0
        private void RenameBackupFile(FileSystemWatcherExtClass FileWatcher, string oldFile, string newFile)
        {
            string originPath = "", newPath = "";

            try
            {
                //Get the full path of the original file
                originPath = oldFile.Replace(FileWatcher.Path, FileWatcher.DestinationDirectory)
                             .Replace("\\\\", "\\");

                newPath = newFile.Replace(FileWatcher.Path, FileWatcher.DestinationDirectory)
                          .Replace("\\\\", "\\");

                //If its a directory, rename it
                if (Directory.Exists(originPath))
                {
                    Directory.Move(originPath, newPath);
                }
                else
                {
                    File.Move(originPath, newPath);
                }
            }
            catch (Exception ex)
            {
                LogAction(new string[] { DateTime.Now.ToString() + " Error copying file from " + originPath + " to "
                                         + newPath + ".", ex.Message });
            }
        }
コード例 #3
0
        private void CopyChangedFile(FileSystemWatcherExtClass FileWatcher, string SourceFilePath)
        {
            string destinationPath = "";

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

                //If it douesnt exist already, create it
                if (!Directory.Exists(Directory.GetParent(destinationPath).ToString()))
                {
                    Directory.CreateDirectory(destinationPath);
                }

                //If the directory does exist, create the corresponding Directory
                //Otherwise just copy the file
                if (Directory.Exists(SourceFilePath))
                {
                    Directory.CreateDirectory(destinationPath);
                }
                else
                {
                    File.Copy(SourceFilePath, destinationPath, true);
                }
            }
            catch (Exception ex)
            {
                LogAction(new string[] { DateTime.Now.ToString() + " Error copying file from " + SourceFilePath + " to " +
                                         destinationPath + ". " + ex.Message });
            }
        }
コード例 #4
0
        private void fileSystemWatcher_Renamed(object sender, RenamedEventArgs e)
        {
            FileSystemWatcherExtClass fwExtended = (FileSystemWatcherExtClass)sender;

            RenameBackupFile(fwExtended, e.OldFullPath, e.FullPath);

            LogAction(new string[] { DateTime.Now.ToString() + " File Renamed: " + e.Name });
        }
コード例 #5
0
        private void fileSystemWatcher_Created(object sender, FileSystemEventArgs e)
        {
            //Revert the generic object into a FileSystemWatcher
            FileSystemWatcherExtClass fwExtended = (FileSystemWatcherExtClass)sender;

            //Copy the file
            CopyChangedFile(fwExtended, e.FullPath);

            //Log changes
            LogAction(new string[] { DateTime.Now.ToString() + " File Created: " + e.Name });
        }
コード例 #6
0
        private FileSystemWatcherExtClass GenerateFileWatcher(string FileSource, bool IncludeSubDirs, string Destination)
        {
            FileSystemWatcherExtClass fwExt = new FileSystemWatcherExtClass(Destination);
            string filePath = "";
            int    charPlace;

            try
            {
                if (Directory.Exists(FileSource))
                {
                    filePath = FileSource;
                }
                else
                {
                    //Char Place stores where the Directories end and the File name
                    charPlace = FileSource.LastIndexOf(@"\");
                    filePath  = FileSource.Substring(0);

                    if (Directory.Exists(filePath))
                    {
                        //FileSystemWatcherExtClass == FilePath
                        fwExt.Path   = filePath;
                        fwExt.Filter = FileSource.Substring(charPlace + 1);
                    }
                }

                fwExt.IncludeSubdirectories = IncludeSubDirs;
            }
            catch (Exception ex)
            {
                LogAction(new string[] { DateTime.Now.ToString() + "Error creating FileWatcher on "
                                         + FileSource + ". " + ex.Message });
            }

            //If the path was valid, return the file watcher
            if (fwExt.Path.Length > 0)
            {
                return(fwExt);
            }
            else
            {
                //Otherwise, return null
                return(null);
            }
        }
コード例 #7
0
 private void fileSystemWatcher_Deleted(object sender, FileSystemEventArgs e)
 {
     FileSystemWatcherExtClass fwExtended = (FileSystemWatcherExtClass)sender;
 }