public static FileFilterIndexEnum FilterIndexFromFile(string _file, FileFilterIndexEnum _prefered = FileFilterIndexEnum.Xml) { if (String.IsNullOrEmpty(_file)) { return(_prefered); } string ext = System.IO.Path.GetExtension(_file).ToLower(); if (ext == ".xml") { return(FileFilterIndexEnum.Xml); } if (ext == ".tol") { return(FileFilterIndexEnum.ListOfTaxons); } return(_prefered); }
public bool Save(bool chooseFile = false, FileFilterIndexEnum _preferredFileExt = FileFilterIndexEnum.Xml) { if (!HasFile) { string message = "Cannot save that list in a file, it has not the HasFile flag set !"; MessageBox.Show(message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } if (chooseFile || String.IsNullOrWhiteSpace(FileName)) { SaveFileDialog sfd = new SaveFileDialog { FileName = FileName, InitialDirectory = GetFolder(), FilterIndex = 2 * (int)_preferredFileExt, Filter = FileFilters, AddExtension = true }; if (sfd.ShowDialog() != DialogResult.OK) { return(false); } FileName = sfd.FileName; } try { XmlSerializer serializer = new XmlSerializer(typeof(TaxonList)); using (TextWriter writer = new StreamWriter(FileName)) { serializer.Serialize(writer, this); } } catch (Exception e) { Loggers.WriteError(LogTags.Data, "Exception while saving file : \n " + FileName + "\n" + e.Message); return(false); } return(true); }