I am trying to create a small web app, that when I type in a game name, it will search through a folder with a whole lot of icons to find one similar to the typed in game.
I then want to show a repeater with the name of the icon, as well as the icon next to it.
So far I have successfully displayed the name with this code:
IEnumerable<string> getfiles =
from f in
Directory.EnumerateFiles(Server.MapPath("~/_resources/game_icon"), "*.*", SearchOption.AllDirectories)
where Path.GetFileName(f).ToLower().Contains(txtGameName.Text.ToLower())
select Path.GetFileNameWithoutExtension(f);
List<AllFiles> all = getfiles.Select(file => new AllFiles
{
FileName = file,
SubdirectoryName = "",
FileNameWithExtension = ""
}).ToList();
rptGameMatches.DataSource = all;
rptGameMatches.DataBind();
But havent had much luck in displaying the icon, which would be in ImageLocation.
Ive tried playing with:
foreach (string file in Directory.EnumerateFiles(Server.MapPath("~/_resources/game_icon"), "*.*", SearchOption.AllDirectories).Count() )
{
if(file.)
}
and
for (int i = 0; i < Directory.EnumerateFiles(Server.MapPath("~/_resources/game_icon"), "*.*", SearchOption.AllDirectories).Count(); i++)
{
}
With no success.
I know there is a simplistic way to do this, but I cant quite seem to get it right. Could someone please show me where I’m going wrong?
Here’s an example of what you could do (for compactness reasons I have placed the code behind and the markup inside a single WebForm but of course you could have the code behind in a separate file):
Alright, so we have a web page containing a text field, a button and a repeater. When the button is clicked we are searching for all images in the specified folder whose name contains the text entered into the text field:
There are a few steps here:
at this stage we are getting the physical location to the file. It will look like this:
Then we strip the application root and replace the slashes:
so
relativeLocationbecomes:and the last step is:
which will turn that into:
So at the end of the day the images collection might look like this (assuming the user searched for
imagein the text field):It is this collection that is bound to the repeater. Inside the repeater we have defined an Image whose
ImageUrlproperty will be data bound to theUrlproperty of the anonymous object we constructed.