// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Invoked by the ProgressDialog OnShown callback private async Task Execute(UI.ProgressDialog progress, CancellationToken token) { logger.Start(); logger.StartClock(); archivist = new Archivist(one, zipPath); await archivist.BuildHyperlinkMap( bookScope?OneNote.Scope.Sections : OneNote.Scope.Pages, progress, token); // use this temp folder as a sandbox for each page var t = Path.GetTempFileName(); tempdir = Path.Combine(Path.GetDirectoryName(t), Path.GetFileNameWithoutExtension(t)); PathFactory.EnsurePathExists(tempdir); logger.WriteLine($"building archive {zipPath}"); progress.SetMaximum(totalCount); progress.SetMessage($"Archiving {totalCount} pages"); using (var stream = new FileStream(zipPath, FileMode.Create)) { using (archive = new ZipArchive(stream, ZipArchiveMode.Create)) { await Archive(progress, hierarchy, hierarchy.Attribute("name").Value); } } Directory.Delete(tempdir, true); progress.Close(); UIHelper.ShowMessage(string.Format(Resx.ArchiveCommand_archived, pageCount, zipPath)); logger.WriteTime("archive complete"); logger.End(); }
private void Export(List <string> pageIDs) { OneNote.ExportFormat format; string path; bool withAttachments; bool useUnderscores; // dialog... using (var dialog = new ExportDialog(pageIDs.Count)) { if (dialog.ShowDialog(owner) != DialogResult.OK) { return; } path = dialog.FolderPath; format = dialog.Format; withAttachments = dialog.WithAttachments; useUnderscores = dialog.UseUnderscores; } // prepare... string ext = null; switch (format) { case OneNote.ExportFormat.HTML: ext = ".htm"; break; case OneNote.ExportFormat.PDF: ext = ".pdf"; break; case OneNote.ExportFormat.Word: ext = ".docx"; break; case OneNote.ExportFormat.XML: ext = ".xml"; break; case OneNote.ExportFormat.Markdown: ext = ".md"; break; case OneNote.ExportFormat.OneNote: ext = ".one"; break; } // export... using (var progress = new UI.ProgressDialog()) { progress.SetMaximum(pageIDs.Count); progress.Show(owner); var archivist = new Archivist(one); foreach (var pageID in pageIDs) { var page = one.GetPage(pageID, OneNote.PageDetail.BinaryData); var title = useUnderscores ? PathFactory.CleanFileName(page.Title).Replace(' ', '_') : page.Title; var filename = Path.Combine(path, title + ext); progress.SetMessage(filename); progress.Increment(); if (format == OneNote.ExportFormat.HTML) { if (withAttachments) { archivist.ExportHTML(page, ref filename); } else { archivist.Export(page.PageId, filename, OneNote.ExportFormat.HTML); } } else if (format == OneNote.ExportFormat.XML) { archivist.ExportXML(page.Root, filename, withAttachments); } else if (format == OneNote.ExportFormat.Markdown) { archivist.ExportMarkdown(page, filename, withAttachments); } else { archivist.Export(page.PageId, filename, format, withAttachments); } } } SaveDefaultPath(path); UIHelper.ShowMessage(string.Format(Resx.SaveAsMany_Success, pageIDs.Count, path)); }