简体   繁体   中英

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:

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.

Without it, your string value will be: \\\\_img\\\\_upload\\\\

For future reading, I would suggest this answer that I found very useful

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