private void btnSaveAs_Click(object sender, EventArgs e) { if (assembly == null) { assembly = new BindingAssembly(); } ctlSettings.ScreenToSettings(assembly.DefaultSettings); FileInfo fi = new FileInfo(txtFileName.Text); SaveFileDialog dlgSaveFile = new SaveFileDialog(); if (fi.Exists) { dlgSaveFile.InitialDirectory = fi.DirectoryName; } dlgSaveFile.FileName = fi.Name; dlgSaveFile.Filter = "Text (*.TXT)|*.txt|XML (*.XML)|*.xml|Manifest (*.Manifest)|*.Manifest|All files (*.*)|*.*"; dlgSaveFile.FilterIndex = 3; dlgSaveFile.FileOk += new System.ComponentModel.CancelEventHandler(DlgSaveFile_FileOk); dlgSaveFile.ShowDialog(); if (!didSelectSaveFile) { return; } FileInfo newFi = new FileInfo(dlgSaveFile.FileName); switch (newFi.Extension.ToLower()) { case ".txt": case ".text": // For text we write a list of files -- only using (FileStream fs = File.Open(newFi.FullName, FileMode.Create, FileAccess.ReadWrite, FileShare.None)) { using (StreamWriter sw = new StreamWriter(fs)) { foreach (BindingAssembly.File file in assembly.Files) { sw.Write(GetRelativePath(dlgSaveFile.FileName, file.FileName)); sw.WriteLine(); } } } break; default: BindingAssembly ba = new BindingAssembly(); ba.CopyFrom(assembly); foreach (BindingAssembly.File file in ba.Files) { file.FileName = GetRelativePath(dlgSaveFile.FileName, file.FileName); } using (FileStream fs = File.Open(newFi.FullName, FileMode.Create, FileAccess.ReadWrite, FileShare.None)) { FileReader.Serialize(assembly, fs); } break; } }
public static BindingAssembly GetAssembly(string fileName, BindingAssembly assembly) { if (string.IsNullOrWhiteSpace(fileName)) { throw new InvalidDataException("Filename may not be null/empty"); } FileInfo fi = new FileInfo(fileName); if (!fi.Exists) { throw new InvalidDataException("Input manifest file must exist"); } if (fi.Length > MaxManifestFileLength) { throw new InvalidDataException($"The manifest file may not be longer than {MaxManifestFileLength} bytes"); } // File exists and we've got a good name, read it string manifestFile = File.ReadAllText(fileName); bool isValidXml = false; XmlDocument doc = new XmlDocument(); try { doc.LoadXml(manifestFile); isValidXml = true; } catch { } if (isValidXml) { // Try to Deserialize try { if (xmlSer == null) { xmlSer = new XmlSerializer(typeof(BindingAssembly)); } BindingAssembly xmlAssembly = null; using (FileStream fs = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) { xmlAssembly = xmlSer.Deserialize(fs) as BindingAssembly; } FixupPaths(fileName, xmlAssembly); assembly.CopyFrom(xmlAssembly); return(assembly); } catch { throw new InvalidDataException("Unable to deserialize XML"); } } else { // Process as list of files string[] lines = manifestFile.Split(new char[] { '\r', '\n' }); foreach (string line in lines) { string cleanLine = line.Trim(); if (cleanLine.Length > 0) { assembly.Files.Add(new BindingAssembly.File(line, assembly.DefaultSettings)); } } } FixupPaths(fileName, assembly); return(assembly); }