4

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;        
Community
  • 1
  • 1
Ghaleon
  • 1,186
  • 5
  • 28
  • 55

6 Answers6

8
if (File.Exists(Path.Combine(path, matriculation + ".jpg"));
Bazzz
  • 26,427
  • 12
  • 52
  • 69
paul
  • 21,653
  • 1
  • 53
  • 54
2

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;
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Thanks Tim ! +1 . What is this `string.Format("{0}.jpg"..)` for ? Is it to replace the `{0}` for `matriculation's value` ? – Ghaleon Feb 04 '13 at 12:13
  • 1
    @Ghaleon It replaces "{0}" with `matriculation`. http://msdn.microsoft.com/en-us/library/system.string.format.aspx – Nolonar Feb 04 '13 at 12:16
  • Yes, it took me sometime to remeber, i saw it in `C` hehe. Thanks Tim ! – Ghaleon Feb 04 '13 at 12:17
  • @TimSchmelter Tim ! I tested now, there's a picture in the folder with the name I pass as parameter, but if keeps returning `false` What's wrong ? – Ghaleon Feb 04 '13 at 13:14
  • @Ghaleon: difficult to tell without more informations. Have you tried to set a breakpoint and use the debugger to check what's going on? Set a breakpoint at `string dir = Txt_PhotosPath.Text;`. Then hit F10 to go to the next line and right-click+quickwatch to inspect the values of the variables(e.g. `Path.Combine(dir, fileName)`). – Tim Schmelter Feb 04 '13 at 13:16
  • @TimSchmelter Yes, I did. I dont know what tell you.. I updated my question with current code that you gave me. It's normal but when gets on `if (File.Exists(Path.Combine(path, filename)))` it returns `false`. Do I need to set an `\` before the filename ? – Ghaleon Feb 04 '13 at 13:18
  • @Ghaleon: I can only tell you that it should work. So you should first copy the file-path(`Path.Combine(path, filename)`) and insert it into the explorer. Then you'll see the complete path. Verify that the file exists there or not. Maybe you think that it exists but actually it has a different extension(e.g. `607659.jpg.jpg`). – Tim Schmelter Feb 04 '13 at 13:24
  • @Ghaleon: Have a look at this answer which has some additional tips: http://stackoverflow.com/a/11771331/284240 – Tim Schmelter Feb 04 '13 at 13:25
  • the `QuickWatch` Gave me this: `Combine The name 'Combine' does not exist in the current context` – Ghaleon Feb 04 '13 at 13:25
  • 1
    @Ghaleon: You have to select what you want to inspect. So if you want to inspect the value of `Path.Combine(path, filename)`, you have to select all before you right-click it. So don't double-click `Combine`. Understand the quick-watch-window as a console where you can enter code, `Combine` would not be a valid statement alone. – Tim Schmelter Feb 04 '13 at 13:34
  • @TimSchmelter Worked, I guess the problem was `blank spaces`. Sorry, my bad! I used `Trim()` and solved the problem. Thanks again Tim ! – Ghaleon Feb 04 '13 at 13:36
1

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.

Behroz Sikander
  • 3,885
  • 3
  • 22
  • 36
1

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

A J
  • 2,112
  • 15
  • 24
0

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.

Nolonar
  • 5,962
  • 3
  • 36
  • 55
  • Thanks +1. I was avoiding to use `string[]` because I thought i'd have to use `foreach` but the `files.lengh > 0` is much simpler hehe :D – Ghaleon Feb 04 '13 at 12:10
0

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
TYY
  • 2,702
  • 1
  • 13
  • 14