Пример #1
0
        /*
         * private void ExportDBF(Action<IEnumerable<FileInfo>> callBack)
         * {
         *  var filesToDownload = GetSelectedFiles();
         *  var disc = DiscoverRepository.GetInstance();
         *  var rep = DiscoverRepository.CurrentRepository;
         *  var convert = new DBConvert(folderBrowserDialog1.SelectedPath);
         *  var download = disc.DownloadCache(rep.Name, DiscoveredRepositoryCache.Files, filesToDownload);
         *  var localFiles = disc.CopyCacheTo(download, folderBrowserDialog1.SelectedPath);
         *  convert.ConvertDBC2DBF(localFiles.ToArray());
         *  foreach (var lf in localFiles)
         *      lf.Delete();
         *  callBack?.Invoke( localFiles.Select(f => new FileInfo(f.FullName.ToLower().TrimEnd('c') + "f"))?.ToArray());
         *  //return convert;
         * }
         *
         * private void ExportLocalDBF(Action<IEnumerable<FileInfo>> callBack)
         * {
         *  var filesToDownload = GetSelectedFiles();
         *  var disc = DiscoverRepository.GetInstance();
         *  var rep = DiscoverRepository.CurrentRepository;
         *  var wk = Path.Combine(@"c:\temp\sus", rep.Name);
         *  if (!Directory.Exists(wk))
         *      Directory.CreateDirectory(wk);
         *  var convert = new DBConvert(wk);
         *  var download = disc.DownloadCache(rep.Name, DiscoveredRepositoryCache.Files, filesToDownload);
         *  var localFiles = disc.CopyCacheTo(download, wk);
         *  convert.ConvertDBC2DBF(localFiles.ToArray());
         *  foreach (var lf in localFiles)
         *      lf.Delete();
         *  callBack?.Invoke(localFiles.Select(f => new FileInfo(f.FullName.ToLower().TrimEnd('c') + "f"))?.ToArray());
         *  //return convert;
         * }*/

        private void btnExport_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.SelectedPath = @"C:\";
            var selected = GetSelectedFiles("dbc");

            if ((selected?.Count() ?? 0) == 0)
            {
                MessageBox.Show("Selecione ao menos um arquivo compatível (.dbc)!", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            var files = GetSelectedFiles().Where(f => f.Name.ToLower().EndsWith("dbc"));

            if (files?.Count() != selected?.Count())
            {
                MessageBox.Show("Apenas arquivos no formato DBC podem ser exportados para outros formatos\r\nOutras extensões serão ignoradas no processo",
                                "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            string type = "";

            FormExport.OpenDialog((format) => {
                type = format;
            });
            if (string.IsNullOrEmpty(type))
            {
                return;
            }
            if (folderBrowserDialog1.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            var workingFolder   = DBConvert.GetWorkingDirectory();
            var filesToDownload = GetSelectedFiles();
            var disc            = DiscoverRepository.GetInstance();
            var rep             = DiscoverRepository.CurrentRepository;

            type = Regex.Match(type, @"\w+", RegexOptions.IgnoreCase).Value.ToUpper();

            IEnumerable <FileInfo> dbfs = null;

            Action callbackExport = () =>
            {
                disc.ExportFiles2DBF(filesToDownload, (d) => dbfs = d, workingFolder);
            };
            var  tableManager = new DBFReader(workingFolder, DBVersion.Default);
            bool tdbf         = false;

            switch (type)
            {
            case "DBF":
                tdbf            = true;
                callbackExport += () => {
                    tdbf = true;
                };
                break;

            case "XML":
                callbackExport += () =>
                {
                    if (dbfs?.Any() != true)
                    {
                        return;
                    }
                    foreach (var dbf in dbfs)
                    {
                        tableManager.ToXML(dbf.Name);
                    }
                };
                break;

            case "CSV":
                callbackExport += () =>
                {
                    if (dbfs?.Any() != true)
                    {
                        return;
                    }
                    foreach (var dbf in dbfs)
                    {
                        tableManager.ToCSV(dbf.Name);
                    }
                };
                break;

            case "JSON":
                callbackExport += () =>
                {
                    if (dbfs?.Any() != true)
                    {
                        return;
                    }
                    foreach (var dbf in dbfs)
                    {
                        tableManager.ToJSON(dbf.Name);
                    }
                };
                break;
            }
            if (!tdbf)
            {
                callbackExport += () =>
                {
                    if (dbfs?.Any() != true)
                    {
                        return;
                    }
                    foreach (var dbf in dbfs)
                    {
                        dbf.Delete();
                    }
                };
            }
            else
            {
                callbackExport += () =>
                {
                    if (dbfs?.Any() != true)
                    {
                        return;
                    }
                    foreach (var dbf in dbfs)
                    {
                        dbf.MoveTo(Path.Combine(folderBrowserDialog1.SelectedPath, dbf.Name));
                    }
                };
            }
            callbackExport += () =>
            {
                tableManager.Dispose();
                //MessageBox.Show("Arquivos exportados com sucesso", "Conversão", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Process.Start("explorer", folderBrowserDialog1.SelectedPath);
            };


            FormProgress.Run(callbackExport, (ex) =>
            {
                if (ex != null)
                {
                    MessageBox.Show("Ocorreu a sequinte excessão:\r\n" + ex, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            });
        }