public string ReadFile(out string fileName, out List <string[]> csvContents) { // Initialise out parameters fileName = ""; csvContents = null; string txtContents = ""; // [0] will be txt filename, [1] will be mpk filename string[] txtCsvPaths = { "", "" }; // Configure open file dialog box OpenFileDialog dlg = new OpenFileDialog(); dlg.Title = "Open an mpk file"; dlg.DefaultExt = ".mpk"; dlg.Filter = "MPK files (.mpk)|*.mpk|All files (*.*)|*.*"; // Show open file dialog box Nullable <bool> result = dlg.ShowDialog(); // Process open file dialog box results if (result == true) { try { // Get filenames from mpk document fileName = dlg.FileName; ReadWriteMPK rwmpk = new ReadWriteMPK(); txtCsvPaths = rwmpk.ReadFile(fileName); // Get text from txt file ReadWrite rw = new ReadWrite(); txtContents = rw.ReadFile(txtCsvPaths[0]); // Get content from csv file ReadWriteCSV rwcsv = new ReadWriteCSV(); csvContents = rwcsv.ReadFile(txtCsvPaths[1]); } catch (Exception) { MessageBox.Show("Error at ReadWriteDialog.ReadFile()"); } } return(txtContents); }
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"); } } }