private void WriteTdsFile(string fileLocation, string [,] readingValueArray) { //this is going to fetch the amount of lines in the document we are writing to string[] lines = File.ReadAllLines(fileLocation); //simple counters for each location on the tube int leftCounter = 0; int centerCounter = 0; int rightCounter = 0; //outputting the array values to the TDS file using (StreamWriter writer = new StreamWriter(fileLocation)) { for (int currentLine = 1; currentLine <= lines.Length; ++currentLine) { //printing left readings to file first if (currentLine >= 6 && currentLine <= (_tubeCount + 5)) { string readingValue = readingValueArray[0, leftCounter]; //passing the cleanup method the value of the cell in the array, it will return the TDS acceptable value string returnedValue = DataValidation.DataValidator(readingValue); writer.WriteLine(returnedValue); leftCounter++; } //printing center readings else if (currentLine > (_tubeCount + 11) && currentLine <= ((_tubeCount * 2) + 11)) { string readingValue = readingValueArray[1, centerCounter]; //passing the cleanup method the value of the cell in the array, it will return the TDS acceptable value string returnedValue = DataValidation.DataValidator(readingValue); writer.WriteLine(returnedValue); centerCounter++; } //printing right readings else if (currentLine > _tubeCount * 2 + 17 && currentLine <= ((_tubeCount * 3) + 17)) { string readingValue = readingValueArray[2, rightCounter]; //passing the cleanup method the value of the cell in the array, it will return the TDS acceptable value string returnedValue = DataValidation.DataValidator(readingValue); writer.WriteLine(returnedValue); rightCounter++; } else {//not 100% sure I think this closes the stream once end of file is reached and saves it also writer.WriteLine(lines[currentLine - 1]); } } } }
//this method sets the inspection info into variables for display private void SetInspectionInfo(string line, int counter) { string newLine; if (counter == 0) { newLine = DataValidation.RemoveNonAlpha(line); _millName = newLine; } if (counter == 1) { newLine = DataValidation.RemoveNonAlpha(line); _boilerName = newLine; } if (counter == 2) { newLine = DataValidation.RemoveNonAlpha(line); _sectionName = newLine; } if (counter == 3) { newLine = DataValidation.RemoveNonAlpha(line); _elevationName2 = newLine; } if (counter == 4) { newLine = DataValidation.RemoveNonAlpha(line); _elevationName1 = newLine; } if (counter == _tubeCount + 6) { newLine = DataValidation.RemoveNonAlpha(line); _location = newLine; } if (counter == _tubeCount + 7) { newLine = DataValidation.RemoveNonAlpha(line); _inspectionYear = newLine; } if (counter == _tubeCount + 10) { newLine = DataValidation.RemoveNonAlpha(line); _elevationName3 = newLine; } }
//gathers the information for the elevation you want to write to private void SelectFileButton_Click(object sender, EventArgs e) { try { // Create an instance of the open file dialog box. OpenFileDialog openFileDialog1 = new OpenFileDialog { // Set filter options and filter index. Filter = "Text Files (.txt)|*.txt|All Files (*.*)|*.*", FilterIndex = 2, Multiselect = true }; // Call the ShowDialog method to show the dialog box. DialogResult userClickedOk = openFileDialog1.ShowDialog(); // Process input if the user clicked OK. if (userClickedOk == DialogResult.OK) { // clear the list view items before writing so that subsequent file selection doesn't keep populating the listbox. Also clear pasted data if any exists. elevationInfoListBox.Items.Clear(); previewListBox.Items.Clear(); ClearPastedData(); // Open the selected file to read. Stream fileStream = openFileDialog1.OpenFile(); using (new StreamReader(fileStream)) { string filename = openFileDialog1.FileName; _fileLocation = filename; } fileStream.Close(); //check to make sure that the file path is not null and that the file is a valid TDS file if (_fileLocation != null && DataValidation.IsTdsFile(_fileLocation)) { PasteDataButton.Enabled = true; // Code that is going to fetch the tube count and other goodies! string line; int counter = 0; StreamReader reader3 = new StreamReader(_fileLocation); while (reader3.ReadLine() != null) { counter++; } _tubeCount = (counter - 18) / 3; reader3.Close(); StreamReader reader2 = new StreamReader(_fileLocation); counter = 0; //This is grabbing the informaion about the elevation that has been selected to be overwritten //it should allow the user to clearly see at a glance what they are about to overwrite while ((line = reader2.ReadLine()) != null) { previewListBox.Items.Add(line); SetInspectionInfo(line, counter); counter++; } //outputting the elevation to the listview for the user to see, elevationInfoListBox.Items.Add("Mill:\t" + _millName + _location); elevationInfoListBox.Items.Add(""); elevationInfoListBox.Items.Add("Boiler:\t" + _boilerName); elevationInfoListBox.Items.Add(""); elevationInfoListBox.Items.Add("Year:\t" + _inspectionYear); elevationInfoListBox.Items.Add(""); elevationInfoListBox.Items.Add("Section:\t" + _sectionName); elevationInfoListBox.Items.Add(""); elevationInfoListBox.Items.Add("Elevation: " + _elevationName1 + _elevationName2 + _elevationName3); elevationInfoListBox.Items.Add(""); elevationInfoListBox.Items.Add("# Tubes:\t" + _tubeCount);//hashtag tubes! reader2.Close(); } else { //could prolly make these message boxes more appealing MessageBox.Show("Please select a valid TDS file. TDS data files should look like this:\n\nXXXXXX.1 OR XXXXX1.1"); } } } catch (Exception ex) { //generic error message, could be improved MessageBox.Show(ex.Message); } }