Find an Image name with File Exists in C #

1

In my database I have a field that is the guide number, and that same one is also an image name ( .jpg or .tif ). The topic, that I have cases where the guide number has several images and in the folder of images you can appear like this:

nro_guia: GUIA00020---->> BASE DE DATOS
          GUIA00020__28042017165654.tif -------->>CARPETA DE IMAGENES

How can I do to be able to validate the existence of the image of that guide?

               foreach (DataRow row in dtImagenes.Rows)
                {
                    string nom_imagen_db = 
                row["NroGuia"].ToString().TrimEnd(' ');
                    var ruta_imagen = Path.Combine(txt_ruta.Text, 
                nom_imagen_db + ".tif");
                    var ruta_imagen_2 = Path.Combine(txt_ruta.Text, 
                nom_imagen_db + ".jpg");

                    if (pgb_cargando.Value <= cantidad_imagen_db)
                    {
                        pgb_cargando.PerformStep();
                    }
                    if (!File.Exists(ruta_imagen) && 
                    !File.Exists(ruta_imagen_2)
                    {
                        string f_guia = row["FechaGuia"].ToString();
                        guias.InsertarGuiasValidadas(nom_imagen_db,
                        Convert.ToDateTime(f_guia),DateTime.Now, "NO");
                    }
                }

This code works only if the image name is the same as the one in the guide.

I hope you can help me.

Thank you.

    
asked by Gonzalo Rios 23.11.2018 в 23:53
source

1 answer

0

You could search for files that match a pattern that you start with the name of the guide. If there is a file, it has an image, for example something along the lines of:

foreach (DataRow row in dtImagenes.Rows)
{
    string nom_imagen_db = row["NroGuia"].ToString().TrimEnd(' ');
    string[] archivos = Directory.GetFiles(txt_ruta.Text, nom_imagen_db + "_*.*");

    if archivos.Length > 0 {
      //si tiene guia
    }
}

This will find any file that you start with the name of the guide plus a script under _ , but you will still find files that are not images. Now you can specialize if you're interested in finding only the extensions you mention. Sure it is not the best because it also recovers the names of the files and then does not use them.

    
answered by 24.11.2018 / 00:34
source