public bool SaveDocument(String fileName, bool includingCacheFile = true) { if (string.IsNullOrEmpty(DocumentUI.Settings.DataSettings.DocumentGuid) || !Equals(DocumentFilePath, fileName)) { SrmDocument docOriginal; SrmDocument docNew; do { docOriginal = Document; docNew = docOriginal.ChangeSettings( docOriginal.Settings.ChangeDataSettings( docOriginal.Settings.DataSettings.ChangeDocumentGuid())); } while (!SetDocument(docNew, docOriginal)); } SrmDocument document = Document; try { using (var saver = new FileSaver(fileName)) { saver.CheckException(); using (var longWaitDlg = new LongWaitDlg(this) { Text = Resources.SkylineWindow_SaveDocument_Saving___, Message = Path.GetFileName(fileName) }) { longWaitDlg.PerformWork(this, 800, progressMonitor => { using (var writer = new XmlWriterWithProgress(saver.SafeName, fileName, Encoding.UTF8, document.MoleculeTransitionCount, progressMonitor) { Formatting = Formatting.Indented }) { XmlSerializer ser = new XmlSerializer(typeof(SrmDocument)); ser.Serialize(writer, document); writer.Flush(); writer.Close(); // If the user has chosen "Save As", and the document has a // document specific spectral library, copy this library to // the new name. if (!Equals(DocumentFilePath, fileName)) SaveDocumentLibraryAs(fileName); saver.Commit(); } }); // Sometimes this catches a cancellation that doesn't throw an OperationCanceledException. if (longWaitDlg.IsCanceled) return false; } } } catch (OperationCanceledException) { return false; } catch (Exception ex) { var message = TextUtil.LineSeparate(string.Format(Resources.SkylineWindow_SaveDocument_Failed_writing_to__0__, fileName), ex.Message); MessageBox.Show(message); return false; } DocumentFilePath = fileName; _savedVersion = document.UserRevisionIndex; SetActiveFile(fileName); // Make sure settings lists contain correct values for this document. document.Settings.UpdateLists(DocumentFilePath); try { SaveLayout(fileName); // CONSIDER: Is this really optional? if (includingCacheFile) { using (var longWaitDlg = new LongWaitDlg(this) { Text = Resources.SkylineWindow_SaveDocument_Optimizing_data_file___, Message = Path.GetFileName(fileName) }) { longWaitDlg.PerformWork(this, 800, () => OptimizeCache(fileName, longWaitDlg)); } } } // We allow silent failures because it is OK for the cache to remain unoptimized // or the layout to not be saved. These aren't critical as long as the document // was saved correctly. catch (UnauthorizedAccessException) {} catch (IOException) {} catch (OperationCanceledException) {} catch (TargetInvocationException) {} return true; }
public static void SaveDocument(SrmDocument doc, string outFile, TextWriter outText) { // Make sure the containing directory is created string dirPath = Path.GetDirectoryName(outFile); if (dirPath != null) Directory.CreateDirectory(dirPath); var progressMonitor = new CommandProgressMonitor(outText, new ProgressStatus(string.Empty)); using (var saver = new FileSaver(outFile)) { saver.CheckException(); using (var writer = new XmlWriterWithProgress(saver.SafeName, outFile, Encoding.UTF8, doc.MoleculeTransitionCount, progressMonitor)) { XmlSerializer ser = new XmlSerializer(typeof(SrmDocument)); ser.Serialize(writer, doc); writer.Flush(); writer.Close(); saver.Commit(); var settings = doc.Settings; if (settings.HasResults) { if (settings.MeasuredResults.IsLoaded) { FileStreamManager fsm = FileStreamManager.Default; settings.MeasuredResults.OptimizeCache(outFile, fsm); //don't worry about updating the document with the results of optimization //as is done in SkylineFiles } } else { string cachePath = ChromatogramCache.FinalPathForName(outFile, null); FileEx.SafeDelete(cachePath, true); } } } }
private void CopyFile(string source, FileSaver destSaver) { // Copy the specified file to the new name using a FileSaver destSaver.CheckException(); File.Copy(source, destSaver.SafeName, true); }