Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check the name of files inside a folder

I tried what I found on this thread but didnt worked exactly the way I wanted... I have a folder named photos it may has pictures or not. The picture's name is the matriculation of the clients. I need to pass the matriculation as parameter and check if there is a picture with the name of the matriculation I passed as parameter

I tried this:

public void VerifyPhoto(string matriculation)
        {
            string path = Txt_PhotosPath.Text;
            var file = Directory.GetFiles(path, matriculation + ".jpg");

        }

How may I check if it found the picture or not ? I tried to compare this, file != null but it does not work with var type. Any tip ? debuging I saw it found the picture because there's a String[1] but I don't know ho to check it...

---Update--- path:C:"\Users\admin\Desktop\photos" matriculation:"607659.jpg" There is a file with that name but it keeps returning false what's wrong?

 string path = Txt_PhotosPath.Text;
            string filename = string.Format("{0}.jpg", matriculation);
            if (Directory.Exists(path))
            {
                if (File.Exists(Path.Combine(path, filename)))
                {
                    return true;
                }
                else
                    return false;
            }
            else
                return false;        
like image 496
Ghaleon Avatar asked Dec 15 '22 14:12

Ghaleon


2 Answers

if (File.Exists(Path.Combine(path, matriculation + ".jpg"));
like image 123
paul Avatar answered Jan 03 '23 03:01

paul


Use Path.Combine and Directory+File.Exists:

public bool VerifyPhoto(string matriculation)
{
    string dir = Txt_PhotosPath.Text;
    if(Directory.Exists(dir))
    {
        string fileName = string.Format("{0}.jpg", matriculation);
        if(File.Exists(Path.Combine(dir, fileName)))
            return true;
        else
            return false;
    }
    else
        return false;
}
like image 27
Tim Schmelter Avatar answered Jan 03 '23 02:01

Tim Schmelter