简体   繁体   English

如何从Visual Studio中的项目/服务器文件夹中获取文件名?

[英]how to get file names from project/server folder in visual studio?

so what i'm trying to do is to get all the file names of all the images that are located in a specific folder in my project or on the server (which is practically the same) in a string array. 所以我想做的就是在字符串数组中获取位于我项目中或服务器上(实际上是相同的)特定文件夹中的所有图像的所有文件名。

I tried with 我尝试过

Directory.GetFiles(Server.MapPath(@"~/_img/_upload/"));

But the array stays empty. 但是数组保持为空。 Could anyone help me with that? 有人可以帮我吗?

I'd appreciate the help, thanks in advance! 感谢您的帮助,在此先感谢!

The below will get all the images into an array (if you already know the directory structure this should help you): 下面将所有图像放入一个数组中(如果您已经知道目录结构,这将对您有所帮助):

using System.IO;
using System.Linq;

var imagenames = String.Join(", ", Directory.GetFiles(@"C:\yourdirectory", "*.img").Select(filename => Path.GetFileNameWithoutExtension(filename)).ToArray());

In base at your comment, the result you got from this Server.MapPath(@"~/_img/_upload/") is: 根据您的评论,从此Server.MapPath(@"~/_img/_upload/")是:

C:\\\\Projects\\\\VS12\\_img\\_upload\\\\

The main problem is the @ because in C# means that the string should be used "as is" and not be processed for escape sequences. 主要问题是@因为在C#中意味着该字符串应“按原样”使用,而不对转义序列进行处理。

Without it, your string value will be: \\\\_img\\\\_upload\\\\ 没有它,您的字符串值将为: \\\\_img\\\\_upload\\\\

For future reading, I would suggest this answer that I found very useful 为了以后阅读,我建议我觉得这个答案非常有用

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

相关问题 如何从 Visual Studio 项目中排除文件/文件夹? - How to exclude file/folder from visual studio project? 用于“安装和部署Visual Studio”的自定义安装项目-如何获取.msi文件所在的文件夹的内容? - Custom setup project for “Setup and Deployment Visual Studio”- How to get the contents of the folder where the .msi file resides? 如何在Visual Studio解决方案的项目中检查文件是否存在于文件夹中 - How to check if file exists in a folder in my project in Visual Studio solution 如何从类文件Visual Studio中获取最近修改的方法名称 - How to get Recently modified Method Names from a class File Visual Studio 如何将设置文件保存到Visual Studio中已发布的文件夹中? - How to get the settings file saved to released folder in Visual Studio? 如何从Visual Studio 2012获取已安装应用程序的文件夹路径 - How to get folder path of Installed application, from Visual Studio 2012 如何从Azure服务器更新Visual Studio项目文件 - How to update Visual Studio project files from Azure server 如何在Visual Studio加载项中获取文件的项目名称? - How to get project name of file in visual studio add-in? 如何在 Visual Studio 项目中获取或设置动态绝对文件路径? - How to get or setup dynamic absolute File Path in Visual Studio Project? 从Visual Studio 2012中的文件夹打开文件 - open file from folder in visual studio 2012
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM