Reputation: 1196
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
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
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
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
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
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