简体   繁体   中英

Creating a relative path to known folder

I'm looking for a way to make a list of files from a relative filepath.

So far I've come up with this:

string[] filePaths = Directory.GetFiles(@"~\Images\Uploaded\");

But I get an error which says that the path doesn't exist, although it does.

If it is an ASP.Net application you can use Server.MapPath to convert relative path to absolute path

string folderPath = Server.MapPath("~\Images\Uploaded\");

From the SO answer

You can use Directory.GetcurrentDirectory to get the current directory, and Path.Combine to combine with relative path to form absolute path and get the files from this absolute path.

Try following

string[] filePaths = Directory.GetFiles(Path.Combine(Directory.GetCurrentDirectory(),@"\Images\Uploaded\"));

or just use the relative path without using ~

string[] filePaths = Directory.GetFiles(@"\Images\Uploaded\"));

I have not tried it myself, but I'm curious about Directory.GetFiles(VirtualPathUtility.GetDirectory(@"~\\Images\\Uploaded\\"));

VirtualPathUtility documentation

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