public string WriteFileAs() { string filename = null; // Configure save file dialog box SaveFileDialog dlg = new SaveFileDialog(); dlg.FileName = "Measurements"; dlg.DefaultExt = ".mpk"; dlg.Filter = "MPK files (.mpk)|*.mpk"; // Show save file dialog box Nullable <bool> result = dlg.ShowDialog(); // Process save file dialog box results if (result == true) { try { // save document ReadWriteMPK rwmpk = new ReadWriteMPK(); filename = dlg.FileName; rwmpk.WriteFile(filename, false); } catch (Exception e) { MessageBox.Show(e.Message, "Error at ReadWriteDialog.WriteFileAsCSV()"); return(null); } } return(filename); }
public void Save(bool isname) { // In case user clicks Save straight away EnableEditing(); // Change save to saveas if needed if (fileName == defaultfileName || fileName == null) { isname = false; } // Store current textCursor position TextPointer textCursorPosition = Window.text.Selection.Start; // Textbox data for txt file Window.text.SelectAll(); string textToSave = Window.text.Selection.Text; // Measurement data for csv file List <string[]> dataToSave = new List <string[]>(); foreach (var c in Window.Data) { double y = Math.Truncate(c.Y * 100) / 100; int place = 0; if (c.Meter.ToString() == "AIRPORT") { place = 0; } else if (c.Meter.ToString() == "CENTER") { place = 1; } else if (c.Meter.ToString() == "RUISSALO") { place = 2; } string[] array = new string[3]; array[0] = c.X.ToString(); array[1] = y.ToString(); array[2] = place.ToString(); dataToSave.Add(array); } ReadWriteDialog rwd = new ReadWriteDialog(); // If there isn't an existing name or user clicked saveAs, do saveAs if (isname == false) { fileName = rwd.WriteFileAs(); } // Else just save with existing filename else { // Call save on mpk file ReadWriteMPK rwmpk = new ReadWriteMPK(); rwmpk.WriteFile(fileName, false); } // Whether we've done Save or SaveAs, there should now be a working filename, and the MPK file is saved // All that's left is to save the txt and csv data to the relevant files if (fileName != null) { try { // Set txt and csv filenames by removing last 3 chars of (mpk) filename and adding txt or csv string filenameBase = fileName.Substring(0, fileName.Length - 3); string txtFilename = filenameBase + "txt"; string csvFilename = filenameBase + "csv"; // Save txt file ReadWrite rw = new ReadWrite(); rw.WriteFile(txtFilename, textToSave, false); // Save csv file ReadWriteCSV rwcsv = new ReadWriteCSV(); rwcsv.WriteFile(csvFilename, dataToSave, false); // Set title Window.Title = fileName; // Put textCursor back in its previous position Window.text.Selection.Select(textCursorPosition, textCursorPosition); } catch (Exception) { MessageBox.Show("Error saving to existing file, try SaveAs instead"); } } }