简体   繁体   English

从我的项目/解决方案中的文件夹中获取所有图像

[英]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? 如何从代码中获取icon1和icon2? (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. 您可以执行类似Server.MapPath("~/Images/HelpIcons")或者在Web项目中使用虚拟路径。

Alternatively, you can get the current execution directory and work your way from there Directory.GetCurrentDirectory() 或者,您可以获取当前的执行目录,然后从该Directory.GetCurrentDirectory()执行工作Directory.GetCurrentDirectory()

see http://msdn.microsoft.com/en-us/library/system.io.directory.getcurrentdirectory(v=vs.71).aspx 参见http://msdn.microsoft.com/zh-cn/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) 一旦你的起点,你让孩子们用目录Directory.GetDirectories(string path) ,并与文件Directory.GetFiles(string path)

You can use GetFiles from the directory you specified. 您可以从指定的目录中使用GetFiles。

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" filenames包含该目录中所有文件filenames的列表,然后您可以从此处查找“ .png”

--Edit 12:13-- -编辑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/ 如果它在代码的已编译部分中,则应该可以直接打到根目录,然后返回目录,例如/ Resources / Images / HelpIcons /

If you are accessing the images from a front end file eg .aspx try ~/Resources/Images/HelpIcons 如果要从前端文件(例如.aspx)访问图像,请尝试〜/ Resources / Images / HelpIcons

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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