简体   繁体   中英

ASP.NET how to get file name in specific folder on server

I have this website which I am coding in ASP.NET - C#.

My problem is that I have image boxes, but the user can upload images with any name. Now each image box has its own folder, and at any point, there can be only one file in that folder, which is a .JPG file.

the path looks like this:

Server.MapPath("img/home/1/here can be any jpg file with any name eg. whateverPic.jpg")

So, when uploading the file, the name can change at any time... then the problem comes when I want to display the image in the picture box.

All I want to do is to get the file name in folder img/home/1/.... and then I can just set the source of the picturebox to that name.

How can I get that filename in that specific folder?

you can use this to get a list of files in the directory

string[] filePaths = Directory.GetFiles(@"c:\MyDir\");

then just use the file name in the filePaths[0] index

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM