Beispiel #1
0
        private void ImportData(object sender, EventArgs e)
        {
#if DEBUG
            Console.WriteLine("Importing into file with {0} entries", EditedFile.Entries.Count);
#endif
            OpenFileDialog openDBFileDialog = new OpenFileDialog {
                InitialDirectory = Settings.Default.ImportExportDirectory,
                FileName         = Settings.Default.TsvFile(EditedFile.CurrentType.Name)
            };

            bool tryAgain = false;
            if (openDBFileDialog.ShowDialog() == DialogResult.OK)
            {
                Settings.Default.ImportExportDirectory = Path.GetDirectoryName(openDBFileDialog.FileName);
                do
                {
                    try {
                        DialogResult question = MessageBox.Show("Replace the current data?", "Replace data?",
                                                                MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                        if (question == DialogResult.Cancel)
                        {
                            return;
                        }
                        DBFile imported;
                        try {
                            using (var stream = new MemoryStream(File.ReadAllBytes(openDBFileDialog.FileName))) {
                                imported = new TextDbCodec().Decode(stream);
                            }
                            if (question == DialogResult.Yes)
                            {
                                EditedFile = imported;
                            }
                            else
                            {
                                EditedFile.Import(imported);
                            }
                        } catch (DBFileNotSupportedException exception) {
                            ShowDBFileNotSupportedMessage(exception.Message);
                        }

                        CurrentPackedFile.Data = (Codec.Encode(EditedFile));
                        Open();
                    } catch (Exception ex) {
                        tryAgain = (MessageBox.Show(string.Format("Import failed: {0}", ex.Message),
                                                    "Import failed",
                                                    MessageBoxButtons.RetryCancel)
                                    == DialogResult.Retry);
                    }
                } while (tryAgain);
            }
        }