private void btnOpenPrj_Click(object sender, EventArgs e) { OpenFileDialog openFileDialogTTP = new OpenFileDialog(); openFileDialogTTP.InitialDirectory = @"C:\Users\Alex\Desktop\UTES\ttng"; //openFileDialogTTP.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); openFileDialogTTP.Filter = "TranslaTale Project files (*.ttp, *.ttpx, *.zip)|*.ttp;*.ttpx;*.zip|All files (*.*)|*.*"; openFileDialogTTP.RestoreDirectory = true; if (openFileDialogTTP.ShowDialog() == DialogResult.OK) { try { TTProject proj = projectHandler.loadProject(openFileDialogTTP.FileName); } catch (Exception ex) { MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message, "Error loading project", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } }
/** * Project loading routine. It checks for the file format and tries to open it * according to the file and TTProject object specifications. * * (TTP - TT Project - XML) * (TTPX - TT Consolidated Project - ZIP) * * @param filePath an absolute URI giving the base location of the project file * @return TTProject TranslaTale Project object */ public static TTProject loadProject(string filePath, bool errorMsgs = true) { byte[] buffer = new byte[2]; XDocument xd1 = new XDocument(); TTProject project = new TTProject("", "", false, false, xd1); try { using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { fs.Read(buffer, 0, buffer.Length); fs.Close(); if (System.Text.Encoding.UTF8.GetString(buffer) == "<?") // if XML { try { project.Path = filePath; project.WorkingPath = filePath; project.ProjConsolidated = false; project.Err = false; project.Properties = XDocument.Load(filePath); // XML OK return(project); } catch (XmlException xex) { if (errorMsgs == true) { MessageBox.Show("The chosen file is not a valid TranslaTale project", "Error loading project", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } project.Path = ""; project.WorkingPath = ""; project.ProjConsolidated = false; project.Err = true; project.Properties = new XDocument(); return(project); } } else if (System.Text.Encoding.UTF8.GetString(buffer) == "PK") // if ZIP { try { if (ZipFile.CheckZip(filePath) == true) // ZIP OK { try { string tmpFolder = ExtractFileToDirectory(filePath); Process.Start(tmpFolder); project.Path = filePath; project.WorkingPath = Path.Combine(tmpFolder, @"project.ttp"); project.ProjConsolidated = true; project.Err = false; project.Properties = XDocument.Load(Path.Combine(tmpFolder, @"project.ttp")); // XML OK return(project); } catch (XmlException xex) { if (errorMsgs == true) { MessageBox.Show("The chosen file is not a valid TranslaTale project", "Error loading project", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } project.Path = ""; project.WorkingPath = ""; project.ProjConsolidated = true; project.Err = true; project.Properties = new XDocument(); return(project); } } else // Malformed ZIP { if (errorMsgs == true) { MessageBox.Show("The chosen file is not a valid TranslaTale project", "Error loading project", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } project.Path = ""; project.WorkingPath = ""; project.ProjConsolidated = true; project.Err = true; project.Properties = new XDocument(); return(project); } } catch (ZipException zex) // Malformed ZIP { if (errorMsgs == true) { MessageBox.Show("The chosen file is not a valid TranslaTale project", "Error loading project", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } project.Path = ""; project.WorkingPath = ""; project.ProjConsolidated = true; project.Err = true; project.Properties = new XDocument(); return(project); } } else { // Not recognised if (errorMsgs == true) { MessageBox.Show("The chosen file is not a valid TranslaTale project", "Error loading project", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } project.Path = ""; project.WorkingPath = ""; project.ProjConsolidated = false; project.Err = true; project.Properties = new XDocument(); return(project); } } } catch (System.UnauthorizedAccessException fex) { // Not readable if (errorMsgs == true) { MessageBox.Show("Error: Could not read file from disk. Original error: " + fex.Message, "Error loading project", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } project.Path = ""; project.WorkingPath = ""; project.ProjConsolidated = false; project.Err = true; project.Properties = new XDocument(); return(project); } }