I am building an asp website. I want to show image on the page, but while I know the image name, I don't know the extension.
So, I want to open the images directory and get the image by its name whatever its extension.
I am building an asp website. I want to show image on the page, but while I know the image name, I don't know the extension.
So, I want to open the images directory and get the image by its name whatever its extension.
Share edited Mar 18, 2009 at 13:46 Dana 33k17 gold badges64 silver badges73 bronze badges asked Mar 18, 2009 at 13:42 Amr ElgarhyAmr Elgarhy 69k70 gold badges192 silver badges310 bronze badges 1- What part of this are you having trouble with? What did you try? What happened? – John Saunders Commented Mar 18, 2009 at 13:44
2 Answers
Reset to default 8You can use the Directory object to get a list of file in a directory
string imageFilename = System.IO.Directory.GetFiles( imageDirectory, name + ".*" ).First();
That's not really how files work -- you need to know the whole name. For example, what happens if there's multiple images with the same base name and different extensions? Compare foo.png
and foo.jpg
, for example.
One suggestion is to try doing a wildcard search on all files with some base name in that directory (that's what bstoney's solution does; see here). If you find exactly one match, you're done. If you get multiple matches, you'll need to make rules about which one wins.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745061712a4609006.html
评论列表(0条)