简体   繁体   中英

Get all images from a folder in my project/solution

A quick question.

How do i in the easiest and securest way get all images from a folder in my solution without having to copy them to the debug folder.

So lets say this is my folder structure:

Project
   Resources (Folder)
       Images (Folder)
          HelpIcons (Folder)
             Icon1.png
             Icon2.png

How do i get the icon1 and icon2 from the code? (without copy to debug)

You can do some like Server.MapPath("~/Images/HelpIcons") or whatever the virtual path is if this is in a web project.

Alternatively, you can get the current execution directory and work your way from there Directory.GetCurrentDirectory()

see http://msdn.microsoft.com/en-us/library/system.io.directory.getcurrentdirectory(v=vs.71).aspx

Once you have the starting point you get children directories with Directory.GetDirectories(string path) and the files with Directory.GetFiles(string path)

You can use GetFiles from the directory you specified.

String[] filenames = System.IO.Directory.GetFiles("Project\\Resources\\Images\\HelpIcons");

filenames contains a list of all filenames in that directory, from here you can then look for ".png"

--Edit 12:13--

This will be starting from the directory where your executable is. You can specify the full path if needed.

String[] filenames = System.IO.Directory.GetFiles("C:\\Project\\Resources\\Images\\HelpIcons");

If it is in a compiled section of code you should be able to just hit the root and then come back up to the directory eg /Resources/Images/HelpIcons/

If you are accessing the images from a front end file eg .aspx try ~/Resources/Images/HelpIcons

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