我正在寻找一种从相对文件路径制作文件列表的方法。
到目前为止,我已经想出了这个:
string[] filePaths = Directory.GetFiles(@"~\Images\Uploaded\");
但是我得到一个错误,表示该路径不存在,尽管它确实存在。
答案 0 :(得分:1)
如果是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\"));
答案 1 :(得分:1)
我自己没有尝试过,但我对Directory.GetFiles(VirtualPathUtility.GetDirectory(@"~\Images\Uploaded\"));