我总是在我的道路上使用Confid.VirtualDir。例如:
Config.VirtualDir + "upload/gallery/image.jpg"这给了我/upload/gallery/image.jpg.的路径例如,在每一页都能很好地工作:
<img style="height: 100px; " src="<%=Config.VirtualDir + "upload/gallery/image.jpg" %>" />工作得很好.
但是Directory.GetFiles.,,,现在我对有问题了
它的作用是:
protected string[] images;
images = Directory.GetFiles(@"C:\website\mysite.com\upload\gallery", "*.jpg");但不包括:
protected string[] images;
string path = Config.VirtualDir + "upload/gallery";
images = Directory.GetFiles(path,"*.jpg");我没有在控制台中看到任何错误.
我也尝试过"upload\gallery",但是我得到了错误“无法识别的转义序列”。
使用@"C:\website\mysite.com\upload\gallery"将公开我的服务器的目录结构,这是我希望避免的。
更新:基于亚历克斯K.的建议。我试过:
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");现在,我很困惑如何使用path到、GetFiles、和realImage path来获取图像。
UPDATE2:让它开始工作。谢谢亚历克斯K。我在下面回答
发布于 2019-03-26 17:56:26
评论中的说明:
代码文件:
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 https://stackoverflow.com/questions/55361851
复制相似问题