Ghaleon
Ghaleon

Reputation: 1196

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;        

Upvotes: 4

Views: 1885

Answers (6)

A J
A J

Reputation: 2140

Its pretty easy stuff. The following function will help you check if a file of the name specified in the parameter exists.

File.Exists(Path)

Namespace: System.IO

This function returns true if a file exists. Otherwise it returns a false. The argument is a string which is the full path of a file to be checked.eg: G:\Folder1\Filder2\File.jpg.

It doesnt throw any exception since it returns false if it doent find the file.

You dont have to combine the path and all, just give in the full path of the file as stated in my example.

For more info click here

Upvotes: 1

TYY
TYY

Reputation: 2716

To answer your question the reason why you can not use != null, is because the underlying code for GetFiles() creates a list and calls the ToArray() extension method.

 return new List<string>(FileSystemEnumerableFactory.CreateFileNameIterator(path, userPathOriginal, searchPattern, includeFiles, includeDirs, searchOption, checkHost)).ToArray();

You would need to use either :-

file.Count()  file.Length

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460138

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;
}

Upvotes: 2

Behroz Sikander
Behroz Sikander

Reputation: 4039

Here is what official documentation says: http://msdn.microsoft.com/en-us/library/wz42302f.aspx

If there are no files, or no files match the searchPattern parameter, this method returns an empty array.

So, an empty array will be returned and instead of checking for NULL check for empty array.

Upvotes: 1

Nolonar
Nolonar

Reputation: 6122

Instead of using var file, use string[] files.

To check if you found any files, do if (files.Length > 0)

It is generally a VERY bad idea to use var, so don't do it if you can avoid it.

Upvotes: 0

paul
paul

Reputation: 22001

if (File.Exists(Path.Combine(path, matriculation + ".jpg"));

Upvotes: 8

Related Questions