public void Remove(MaintenanceConfiguration.ArchiveFoldersConfiguration.Folder folder)
 {
     if (folder == null)
     {
         throw new ArgumentNullException("folder");
     }
     this.EnsureFolders();
     MaintenanceConfiguration.ArchiveFoldersConfiguration.Folder[] folders     = this.Folders;
     MaintenanceConfiguration.ArchiveFoldersConfiguration.Folder[] folderArray = new MaintenanceConfiguration.ArchiveFoldersConfiguration.Folder[] { folder };
     this.Folders = ((IEnumerable <MaintenanceConfiguration.ArchiveFoldersConfiguration.Folder>)folders).Except <MaintenanceConfiguration.ArchiveFoldersConfiguration.Folder>((IEnumerable <MaintenanceConfiguration.ArchiveFoldersConfiguration.Folder>)folderArray).ToArray <MaintenanceConfiguration.ArchiveFoldersConfiguration.Folder>();
 }
 public void Add(Func <MaintenanceConfiguration.ArchiveFoldersConfiguration.Folder, MaintenanceConfiguration.ArchiveFoldersConfiguration.FolderHandlers, MaintenanceConfiguration.ArchiveFoldersConfiguration.FolderHandler> folder)
 {
     if (folder == null)
     {
         throw new ArgumentNullException("folder");
     }
     MaintenanceConfiguration.ArchiveFoldersConfiguration.Folder folder1 = new MaintenanceConfiguration.ArchiveFoldersConfiguration.Folder()
     {
         //Handler = folder(folder1, new MaintenanceConfiguration.ArchiveFoldersConfiguration.FolderHandlers())
     };
     this.EnsureFolders();
     MaintenanceConfiguration.ArchiveFoldersConfiguration.Folder[] folders     = this.Folders;
     MaintenanceConfiguration.ArchiveFoldersConfiguration.Folder[] folderArray = new MaintenanceConfiguration.ArchiveFoldersConfiguration.Folder[] { folder1 };
     this.Folders = ((IEnumerable <MaintenanceConfiguration.ArchiveFoldersConfiguration.Folder>)folders).Append <MaintenanceConfiguration.ArchiveFoldersConfiguration.Folder>(folderArray).ToArray <MaintenanceConfiguration.ArchiveFoldersConfiguration.Folder>();
 }
        public override void Execute(MaintenanceWorkItem workItem, ITaskExecutionContext context)
        {
            int j;

            MaintenanceConfiguration.ArchiveFoldersConfiguration.Folder[] enabledFolders = workItem.Configuration.ArchiveFolders.GetEnabledFolders();
            for (int num = 0; num < (int)enabledFolders.Length; num++)
            {
                MaintenanceConfiguration.ArchiveFoldersConfiguration.Folder folder = enabledFolders[num];
                context.Log.Message("Folder: {0}", new object[] { folder });
                FileInfo[]      files   = folder.GetFiles() ?? new FileInfo[0];
                DirectoryInfo[] folders = folder.GetFolders() ?? new DirectoryInfo[0];
                if (files.Length != 0 || folders.Length != 0)
                {
                    MaintenanceConfiguration.ArchiveFoldersConfiguration.Folder folder1 = folder;
                    ArchiveCreated archiveCreated = this._archiver.Archive(folder.ArchiveOptions.Name, (BeginArchive a) => {
                        int i;
                        if (folder1.ArchiveOptions.Expires.HasValue)
                        {
                            a.Options.ExpiresOn(folder1.ArchiveOptions.Expires.Value);
                        }
                        a.Options.GroupedBy(folder1.ArchiveOptions.GroupName);
                        FileInfo[] fileInfoArray = files;
                        for (i = 0; i < (int)fileInfoArray.Length; i++)
                        {
                            a.IncludeFile(fileInfoArray[i]);
                        }
                        DirectoryInfo[] directoryInfoArray = folders;
                        for (i = 0; i < (int)directoryInfoArray.Length; i++)
                        {
                            a.IncludeFolder(directoryInfoArray[i]);
                        }
                    });
                    context.Log.Message("{0} file(s) and {1} folder(s) have been archived and will now be physically deleted. {2}.", new object[] { (int)files.Length, (int)folders.Length, archiveCreated });
                    FileInfo[] fileInfoArray1 = files;
                    for (j = 0; j < (int)fileInfoArray1.Length; j++)
                    {
                        FileInfo fileInfo = fileInfoArray1[j];
                        try
                        {
                            fileInfo.Delete();
                        }
                        catch (Exception exception1)
                        {
                            Exception exception = exception1;
                            throw new Exception(string.Format("Unable to delete file '{0}'.", fileInfo.FullName), exception);
                        }
                    }
                    DirectoryInfo[] directoryInfoArray1 = folders;
                    for (j = 0; j < (int)directoryInfoArray1.Length; j++)
                    {
                        DirectoryInfo directoryInfo = directoryInfoArray1[j];
                        try
                        {
                            directoryInfo.Delete(true);
                        }
                        catch (Exception exception3)
                        {
                            Exception exception2 = exception3;
                            throw new Exception(string.Format("Unable to delete folder '{0}'.", directoryInfo.FullName), exception2);
                        }
                    }
                }
            }
        }
示例#4
0
        public override void Execute(ITaskExecutionContext <MaintenanceWorkItem> context)
        {
            MaintenanceConfiguration.ArchiveFoldersConfiguration.Folder[] folders =
                context.WorkItem.Configuration.ArchiveFolders.GetEnabledFolders();

            foreach (MaintenanceConfiguration.ArchiveFoldersConfiguration.Folder folder in folders)
            {
                context.Log.Message("Folder: {0}", folder);

                FileInfo[]      filesToArchive   = folder.GetFiles() ?? new FileInfo[0];
                DirectoryInfo[] foldersToArchive = folder.GetFolders() ?? new DirectoryInfo[0];

                if (filesToArchive.Length == 0 && foldersToArchive.Length == 0)
                {
                    continue;
                }

                MaintenanceConfiguration.ArchiveFoldersConfiguration.Folder localFolder = folder;

                ArchiveCreated archive = _archiver.Archive(folder.ArchiveOptions.Name, a =>
                {
                    if (localFolder.ArchiveOptions.Expires.HasValue)
                    {
                        a.Options.ExpiresOn(localFolder.ArchiveOptions.Expires.Value);
                    }

                    if (localFolder.ArchiveOptions.CompressionLevel.HasValue)
                    {
                        a.Options.Compression(localFolder.ArchiveOptions.CompressionLevel.Value);
                    }

                    a.Options.GroupedBy(localFolder.ArchiveOptions.GroupName);

                    foreach (FileInfo fileToArchive in filesToArchive)
                    {
                        a.IncludeFile(fileToArchive);
                    }

                    foreach (DirectoryInfo folderToArchive in foldersToArchive)
                    {
                        a.IncludeFolder(folderToArchive);
                    }
                });

                context.Log.Message("{0} file(s) and {1} folder(s) have been archived and will now be physically deleted. {2}.",
                                    filesToArchive.Length, foldersToArchive.Length, archive);

                foreach (FileInfo fileToDelete in filesToArchive)
                {
                    try
                    {
                        fileToDelete.Delete();
                    }
                    catch (Exception ex)
                    {
                        context.Log.Error(folder.Target, $"Unable to delete file '{fileToDelete.FullName}'. Message: {ex.Message}");
                    }
                }

                foreach (DirectoryInfo folderToDelete in foldersToArchive)
                {
                    try
                    {
                        folderToDelete.Delete(recursive: true);
                    }
                    catch (Exception ex)
                    {
                        context.Log.Error(folder.Target, $"Unable to delete folder '{folderToDelete.FullName}'. Message: {ex.Message}");
                    }
                }
            }
        }