public void Export(string[] collectionNames, string outpath, ExportFormat exportFormatSelected) { if (!Directory.Exists(outpath)) { throw new ArgumentException("output path doesn't exist", nameof(outpath)); } IImportExportFormatter formatter = ImportExportFormatterFactory.Create(exportFormatSelected); if (formatter == null) { throw new ArgumentException("Can't find appropriate formatter for " + exportFormatSelected, nameof(exportFormatSelected)); } IMagicDatabaseReadOnly magicDatabase = MagicDatabaseManager.ReadOnly; foreach (string collectionName in collectionNames) { ICardCollection cardcollection = magicDatabase.GetCollection(collectionName); IEnumerable <ICardInCollectionCount> cardsInCollection = magicDatabase.GetCardCollection(cardcollection); if (cardsInCollection == null) { throw new ImportExportException("Can't find collection named {0}", collectionName); } string filePath = Path.Combine(outpath, collectionName + formatter.Extension); try { using (StreamWriter sw = new StreamWriter(filePath, false)) { sw.Write(formatter.ToFile(cardsInCollection)); } } catch (ImportExportException) { if (File.Exists(filePath)) { File.Delete(filePath); } throw; } } }