private void openToolStripMenuItem_Click(object sender, EventArgs e) { using (OpenFileDialog openFileDialog = new OpenFileDialog()) { openFileDialog.Filter = "0.bin/0.txt (*.bin, *.txt)|*.bin;*.txt|All files (*.*)|*.*"; openFileDialog.FilterIndex = 1; openFileDialog.RestoreDirectory = true; openFileDialog.Title = "open msgData.bin (0.bin) or a converted text file"; if (openFileDialog.ShowDialog() == DialogResult.OK) { //Get the path of specified file filePath = openFileDialog.FileName; nameOfFile = Path.GetFileName(filePath); var fileExtension = Path.GetExtension(filePath); if (fileExtension == ".bin" || fileExtension == ".BIN") { savebinToolStripMenuItem.Visible = false; savebinToolStripMenuItem.Enabled = false; savetxtToolStripMenuItem.Visible = true; savetxtToolStripMenuItem.Enabled = true; //Read the contents of the file into a stream var fileStream = openFileDialog.OpenFile(); currentmsgData = new msgDataFile(); currentmsgData.ReadmsgData(fileStream); txtArrayListBox.Items.Clear(); txtArrayListBox.Enabled = false; txtArrayListBox.Visible = false; stringsListBox.Enabled = true; stringsListBox.Visible = true; int k = 0; foreach (var entry in currentmsgData.msgDataMessages) { stringsListBox.Items.Add("String #" + k); k++; } } else if (fileExtension == ".txt" || fileExtension == ".TXT") { savebinToolStripMenuItem.Visible = true; savebinToolStripMenuItem.Enabled = true; savetxtToolStripMenuItem.Visible = false; savetxtToolStripMenuItem.Enabled = false; string[] readText = File.ReadAllLines(filePath, Encoding.UTF8); stringsListBox.Items.Clear(); stringsListBox.Enabled = false; stringsListBox.Visible = false; txtArrayListBox.Enabled = true; txtArrayListBox.Visible = true; msgDataStrings = new List <String>(); int k = 0; foreach (string s in readText) { msgDataStrings.Add(readText[k]); txtArrayListBox.Items.Add("String #" + k); k++; } } else { MessageBox.Show("This file is not a valid message binary or text file", "Invalid file"); } } } }
public void ConvertFile(String fileToConvert) { var fileExtension = Path.GetExtension(fileToConvert); if (fileExtension == ".bin" || fileExtension == ".BIN") { //Read the contents of the file into a stream var fileStream = File.Open(fileToConvert, FileMode.Open); currentmsgData = new msgDataFile(); currentmsgData.ReadmsgData(fileStream); using (SaveFileDialog saveFileDialog1 = new SaveFileDialog()) { saveFileDialog1.Filter = "text file (*.txt)|*.txt|All files (*.*)|*.*"; saveFileDialog1.FilterIndex = 1; saveFileDialog1.RestoreDirectory = true; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { displayTXTBox.Text += "Converting binary file to .txt:\r\n" + fileToConvert + "\r\n"; var savePath = saveFileDialog1.FileName; File.WriteAllLines(savePath, currentmsgData.msgDataMessages, utf8WithoutBom); MessageBox.Show("File saved to: " + savePath, "File saved"); } } } else if (fileExtension == ".txt" || fileExtension == ".TXT") { string[] readText = File.ReadAllLines(fileToConvert, Encoding.UTF8); msgDataStrings = new List <String>(); int k = 0; foreach (string s in readText) { msgDataStrings.Add(readText[k]); k++; } using (SaveFileDialog saveFileDialog1 = new SaveFileDialog()) { saveFileDialog1.Filter = "0.bin (*.bin)|*.bin|All files (*.*)|*.*"; saveFileDialog1.FilterIndex = 1; saveFileDialog1.RestoreDirectory = true; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { var savePath = saveFileDialog1.FileName; using (EndianBinaryWriter newMsgData = new EndianBinaryWriter(File.Open(savePath, FileMode.Create, FileAccess.Write), utf8WithoutBom, false, Endianness.Little)) { displayTXTBox.Text += "Converting file text file to P5S message binary\r\n:" + fileToConvert + "\r\n"; currentmsgData = new msgDataFile(); currentmsgData.WritemsgData(newMsgData, msgDataStrings); MessageBox.Show("File saved to: " + savePath, "File saved"); } } } } else { MessageBox.Show("This file is not a valid message binary or text file", "Invalid file"); } }