【发布时间】:2013-11-16 03:24:28
【问题描述】:
我正在寻找一种从相对文件路径创建文件列表的方法。
到目前为止,我想出了这个:
string[] filePaths = Directory.GetFiles(@"~\Images\Uploaded\");
但我收到一条错误消息,提示该路径不存在,但它确实存在。
【问题讨论】:
我正在寻找一种从相对文件路径创建文件列表的方法。
到目前为止,我想出了这个:
string[] filePaths = Directory.GetFiles(@"~\Images\Uploaded\");
但我收到一条错误消息,提示该路径不存在,但它确实存在。
【问题讨论】:
如果是 ASP.Net 应用程序,您可以使用 Server.MapPath 将相对路径转换为绝对路径
string folderPath = Server.MapPath("~\Images\Uploaded\");
可以使用Directory.GetcurrentDirectory获取当前目录,Path.Combine结合相对路径形成绝对路径,从该绝对路径获取文件。
尝试关注
string[] filePaths = Directory.GetFiles(Path.Combine(Directory.GetCurrentDirectory(),@"\Images\Uploaded\"));
或者只使用相对路径而不使用~
string[] filePaths = Directory.GetFiles(@"\Images\Uploaded\"));
【讨论】:
我自己没试过,但是很好奇Directory.GetFiles(VirtualPathUtility.GetDirectory(@"~\Images\Uploaded\"));
【讨论】: