public void exportCurrentFile(string file, CardFileFormat.Format format) { string filter; switch (format) { case CardFileFormat.Format.TXT: filter = "Text Files (*.txt)|*.txt"; break; case CardFileFormat.Format.CSV: filter = "CSV Files (*.csv)|*.csv"; break; case CardFileFormat.Format.NONE: filter = "CSV Files (*.csv)|*.csv|Text Files (*.txt)|*.txt"; break; default: throw new ArgumentException("Not supported export format."); } _decodeForm.SaveFileDialog.FileName = file; _decodeForm.SaveFileDialog.Filter = filter; //saveFileDialog.AutoUpgradeEnabled = true; if (_decodeForm.SaveFileDialog.ShowDialog() == DialogResult.OK) { string filename = _decodeForm.SaveFileDialog.FileName; //file will be saved depending on file extension saveCurrentFile(filename, format, true); } }
public bool saveCurrentFile(string filename, CardFileFormat.Format format, bool export) { int fields = 0; CardWriter cardWriter = null; try { cardWriter = new CardWriter(filename, format); for (int i = 0; i < cardWriter.Fields.Length; i++) { //column order must be the same as fields order!! if (_decodeForm.CardsGrid.Columns[i].Visible) { fields |= cardWriter.Fields[i]; } } cardWriter.Write(_decodeForm.currentKey, _decodeForm.CardsGrid.GetCardsFromGrid(), fields); if (!export) { _decodeForm.CurrentFileFormat = cardWriter.FileFormat; _decodeForm.CurrentFileName = filename; } else { _decodeForm.openExportedTextFile(filename); } } catch (Exception ex) { Console.Write(ex.Message); TaskDialog.MessageBox(_decodeForm.DecodeForm, "Error", "Cannot save file: " + filename, frmDecode.messageSaveFailed, "", TaskDialogButtons.OK, SysIcons.Error ); return(false); } finally { if (cardWriter != null) { cardWriter.Close(); } } _decodeForm.isCrfModified = false; return(true); }
/** * Constructor. * * @param outputStream Stream where to save cards. * @param saveFormat Format to save file. Only TXT is supported at the moment. * * @warning Only csv, crf and text formats are supported at the moment. If file has any other format * it will be saved as text. */ public CardWriter(MemoryStream outputStream, CardFileFormat.Format saveFormat) { _csvStream = null; _txtStream = null; _crfStream = null; //what format do we have to save? if (saveFormat != CardFileFormat.Format.TXT) { throw new ArgumentException("Only CardFileFormat.Format.TXT is supported with stream output"); } _txtStream = new TxtWriter(outputStream); }
public void exportToFile(string file, CardFileFormat.Format format) { FileInfo fileInfo = new FileInfo(file); if (fileInfo.Exists) { if (MessageBox.Show(_decodeForm.DecodeForm, "File already exists. Do you want to replace it?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.No) { exportCurrentFile(fileInfo.Name, format); return; } } saveCurrentFile(file, format, true); }
/** * Constructor. * * @param file File name where cards will be saved. If file exists its content will be erased. * If file does not exists it will be created. * @param saveFormat Format to save file. If NONE format will be chosen depending on file extension. * * @warning Only csv, crf and text formats are supported at the moment. If file has any other format * it will be saved as text. */ public CardWriter(string file, CardFileFormat.Format saveFormat) { _csvStream = null; _txtStream = null; _crfStream = null; //what format do we have to save? if (saveFormat == CardFileFormat.Format.NONE) { if (file.EndsWith(Program.extension)) { saveFormat = CardFileFormat.Format.CRF; } else if (file.EndsWith(".csv")) { saveFormat = CardFileFormat.Format.CSV; } else { saveFormat = CardFileFormat.Format.TXT; } } if (saveFormat == CardFileFormat.Format.CRF) { _crfStream = new CrfWriter(file); } else if (saveFormat == CardFileFormat.Format.CSV) { _csvStream = new CsvWriter(file); } else //use txt as default { _txtStream = new TxtWriter(file); } }
/** * This function is static because caller needs to know if a password is needed for a file * before opening the file. once file is open it is deleted if it already exists, then user * can cancel password popup but file is already deleted. */ public static bool NeedPassword(CardFileFormat.Format format) { //password is only needed for crf format. return(format == CardFileFormat.Format.CRF); }