Compare names between a SQL-SERVER field and a windows folder - C # - Windows Forms

0

I have created an application that allows me to validate the existence of images with any type of extension using the numero_guia(GUIA001__789656.tif) as a pattern.

Currently my app works without problems, but I would like to improve it.

I will explain a little, what I do here is to obtain guide numbers from the database and compare them in a path of images string[] archivos = Directory.GetFiles(ruta, nom_imagen_db + ".*"); .

Is it possible to save all the image names of a folder in a array and perform the comparison within my foreach ? and thus prevent you from consulting the image folder that is hosted on a server, and speed up the process.

Currently a list of 550000 records takes 1 hour and 40 minutes. With this, would you accelerate the validation process? I hope you can help me. Thanks.

try
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            //INICIO FOR
            int i = 0;
            foreach (DataRow row in dtImagenes.Rows)
            {
                if (!worker.CancellationPending)
                {
                    string nom_imagen_db = row["NroGuia"].ToString().TrimEnd(' ');
                    string[] archivos = Directory.GetFiles(ruta, nom_imagen_db + ".*");
                    string[] archivos2 = Directory.GetFiles(ruta, nom_imagen_db + "_*");
                    if (archivos.Length == 0 && archivos2.Length == 0)
                    {
                        string f_guia = row["FechaGuia"].ToString();
                        guias.InsertarGuiasValidadas(nom_imagen_db,
                        Convert.ToDateTime(f_guia), DateTime.Now, "NO");
                        cantidad_guias_sin_img++;
                        nro_guia_correo = nro_guia_correo + "<br />" + nom_imagen_db;
                    }
                    else
                    {
                        cantidad_guias_con_img++;
                    }
                    worker.ReportProgress(i++);
                }
                else
                {
                    e.Cancel = true;
                    break;
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString());
        }
    
asked by Gonzalo Rios 03.12.2018 в 17:43
source

1 answer

0

Outside the cycle you could declare your arrangements

string[] archivos = Directory.GetFiles(ruta).ToList().Where(z => z.IndexOf('.')>-1);
string[] archivos2 = Directory.GetFiles(ruta).ToList().Where(z => z.IndexOf('_')>-1);

This way you store the names of the documents in memory and no longer list them every time you click on it

    
answered by 03.12.2018 в 18:30