c# - How to get image with its name and any extension? - Stack Overflow

I am building an asp website.I want to show image on the page, but while I know the image name, I don

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
Add a ment  | 

2 Answers 2

Reset to default 8

You 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

相关推荐

  • c# - How to get image with its name and any extension? - Stack Overflow

    I am building an asp website.I want to show image on the page, but while I know the image name, I don

    5小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信