I Have always used Confid.VirtualDir
for my paths. For example:
Config.VirtualDir + "upload/gallery/image.jpg"
This gives me the path of /upload/gallery/image.jpg . which works totally fine in every page for example:
<img style="height: 100px; " src="<%=Config.VirtualDir + "upload/gallery/image.jpg" %>" />
works perfectly fine.
BUT now I have problem with Directory.GetFiles .
It works on:
protected string[] images;
images = Directory.GetFiles(@"C:\website\mysite.com\upload\gallery", "*.jpg");
But not with:
protected string[] images;
string path = Config.VirtualDir + "upload/gallery";
images = Directory.GetFiles(path,"*.jpg");
I don't see any errors in console.
I also tried "upload\\gallery"
but i get error "Unrecognize escape sequence".
Using @"C:\\website\\mysite.com\\upload\\gallery"
, will expose directory structure of my server, which I'd rather to avoid.
UPDATE : Based on Alex K. advice. I tried:
protected string[] images;
protected string realImage;
string path = Server.MapPath(@"~\upload\Gallery");
images = Directory.GetFiles(path, "*.jpg");
realImage = Path.Combine(Server.MapPath(@"~\upload\Gallery"), Config.VirtualDir + "upload/Gallery");
Now I'm Confused how to use path to GetFiles and realImage path to get my images.
UPDATE2 : Got it to work. Thanks Alex K . I Answer below
Description in comments:
Code File:
public List<string> images = new List<string>();
protected string realPath;
protected void Page_Load(object sender, EventArgs e)
{
string path = Server.MapPath(@"~\upload\Gallery");
// this gets the actual directory path.
realPath = Path.Combine(path, Config.VirtualDir + "upload/Gallery/");
//this changes the path to the VirtualDir path
DirectoryInfo di = new DirectoryInfo(path);
//this gets file names in the folder
foreach(var fi in di.GetFiles())
{
images.Add(fi.Name);
//adds file names to images list
}
}
HTML:
<img style="height: 100px; " src="<%=realPath + images[0]%>" />
//any images[i] is accessible
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.