Comprobar si el archivo existe en el Álbum

I'm working on simple image manipulation program. I'm currently in phase where I have to save images: I'm doing something like that at the moment: fuente externa

But there is a problem. Every time I want to save Image I'll override previous image. How to check if file exists in album "Saved Pictures"?

preguntado el 06 de septiembre de 12 a las 17:09

2 Respuestas

Assuming you know the file name, you can use something like this:

using (var ml = new MediaLibrary())
{
    using (var pics = ml.SavedPictures)
    {
        using (var img = pics.LastOrDefault(pic => pic.Name == FILENAME))
        {
            if (img == null)
            {
                // file doesn't exist
            }
            else
            {
                // file does exist
            }
        }
    }
}

Respondido el 06 de Septiembre de 12 a las 18:09

why did you use LastOrDefault() instead of FirstOrDefault? That way iterate the full collection, and all you wanted is to check if it does exist... Even so, given that we just want a boolean, I personally would just use Any() instead :) - Pedro Lamas

try this one. to find a file in pictureAlbum

using (var library = new MediaLibrary())
{
   var appFolder = library.RootPictureAlbum.Albums.FirstOrDefault(al => al.Name == "folderName");
    if (appFolder != null && appFolder.Pictures.Count > 0)
    {
        var file = appFolder.Pictures.FirstOrDefault(pc => pc.Name == ("fileName"));
        if (file == null)
        {
            // file doesn't exist
        }
        else
        {
            // file does exist
        }
    }
}

Respondido 17 Feb 16, 07:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.