private void OpenFile(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal); openFileDialog.Filter = "LadderApp files (*.xml;*.a43)|*.xml;*.a43|XML files (*.xml)|*.xml|MSP430 Executable files (*.a43)|*.a43"; if (openFileDialog.ShowDialog(this) == DialogResult.OK) { string fileName = openFileDialog.FileName; switch (Path.GetExtension(fileName).ToLower()) { case ".xml": try { XmlReader fileReader = new XmlTextReader(new FileStream(fileName, FileMode.Open)); XmlSerializer xmlSerializer = new XmlSerializer(typeof(LadderProgram)); if (xmlSerializer.CanDeserialize(fileReader)) { LadderProgram ladderProgram = (LadderProgram)xmlSerializer.Deserialize(fileReader); projectForm = new ProjectForm(ladderProgram, AddressingServices.Instance); projectForm.Status = ProjectForm.ProgramStatus.Open; projectForm.PathFile = fileName; projectForm.Program.Name = Path.GetFileNameWithoutExtension(fileName); projectForm.MdiParent = this; projectForm.Show(); projectForm.SetText(); } fileReader.Close(); } catch (Exception ex) { MessageBox.Show("Error reading file! " + ex.InnerException.Message, "Open files ...", MessageBoxButtons.OK, MessageBoxIcon.Error); } break; case ".a43": try { MicIntegrationServices p = new MicIntegrationServices(); String readContent = p.ConvertHex2String(fileName); if (CheckPassword(readContent)) { ReadExecutable(readContent, fileName.Substring(fileName.LastIndexOf(@"\") + 1, fileName.Length - fileName.LastIndexOf(@"\") - 1)); } } catch { MessageBox.Show("Unknown file format!", "Open files ...", MessageBoxButtons.OK, MessageBoxIcon.Error); } break; default: MessageBox.Show("Unknown file format!", "Open files ...", MessageBoxButtons.OK, MessageBoxIcon.Error); break; } } }
private void ReadExecutable(string content, string projectName) { try { ExecutableReaderService executableReaderService = new ExecutableReaderService(); LadderProgram program = executableReaderService.ReadExecutable(content, projectName); projectForm = new ProjectForm(program, AddressingServices.Instance); projectForm.MdiParent = this; projectForm.Show(); projectForm.SetText(); } catch (Exception ex) { MessageBox.Show(ex.Message, "ReadExecutable ...", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void Save(String FileName) { try { XmlSerializer xmlSerializer = new XmlSerializer(typeof(LadderProgram)); StreamWriter fsWriter = new StreamWriter(FileName); xmlSerializer.Serialize(fsWriter, projectForm.Program); fsWriter.Close(); projectForm.PathFile = FileName; projectForm.Program.Name = Path.GetFileNameWithoutExtension(FileName); projectForm.Status = ProjectForm.ProgramStatus.Saved; projectForm.SetText(); } catch (Exception ex) { MessageBox.Show("The file cannot be saved!" + GetMessage(ex), "Salve As ...", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void ShowNewForm(object sender, EventArgs e) { if (MdiChildren.Length == 0) { projectForm = new ProjectForm(new LadderProgram(), addressingServices); projectForm.MdiParent = this; projectForm.Show(); projectForm.SetText(); } else { DialogResult result = MessageBox.Show(VisualResources.SaveTheProjectQuestion.Replace("%%", projectForm.Text.Trim()).Trim(), "LadderApp", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { projectForm.Close(); } } }