首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Directory.GetFiles路径问题

Directory.GetFiles路径问题
EN

Stack Overflow用户
提问于 2019-03-26 16:21:32
回答 1查看 239关注 0票数 0

我总是在我的道路上使用Confid.VirtualDir。例如:

代码语言:javascript
复制
Config.VirtualDir + "upload/gallery/image.jpg"

这给了我/upload/gallery/image.jpg.的路径例如,在每一页都能很好地工作:

代码语言:javascript
复制
<img style="height: 100px; " src="<%=Config.VirtualDir + "upload/gallery/image.jpg" %>" />

工作得很好.

但是Directory.GetFiles.,,现在我对有问题了

它的作用是:

代码语言:javascript
复制
protected string[] images;
images = Directory.GetFiles(@"C:\website\mysite.com\upload\gallery", "*.jpg");

但不包括:

代码语言:javascript
复制
protected string[] images;
string path = Config.VirtualDir + "upload/gallery";
images = Directory.GetFiles(path,"*.jpg");

我没有在控制台中看到任何错误.

我也尝试过"upload\gallery",但是我得到了错误“无法识别的转义序列”。

使用@"C:\website\mysite.com\upload\gallery"将公开我的服务器的目录结构,这是我希望避免的。

更新:基于亚历克斯K.的建议。我试过:

代码语言:javascript
复制
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。我在下面回答

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-26 17:56:26

评论中的说明:

代码文件:

代码语言:javascript
复制
       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:

代码语言:javascript
复制
    <img style="height: 100px; " src="<%=realPath + images[0]%>" />
    //any images[i] is accessible 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55361851

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档