获取所有文件的名称

时间:2014-06-23 16:16:47

标签: c# asp.net-mvc

在ASP.NET MVC项目中,我需要加载项目中所有文件的名称,所以我尝试了:

String[] files = Directory.GetFiles("~", @"*", SearchOption.AllDirectories);

但是我收到以下错误:

An exception of type 'System.IO.DirectoryNotFoundException' occurred in mscorlib.dll but was not handled in user code

Additional information: Could not find a part of the path 'C:\Program Files (x86)\IIS Express\~'.  

我该怎么做?

1 个答案:

答案 0 :(得分:3)

ASP.net的~符号描述了应用程序根目录的路径。您必须使用Server.MapPath("~")来获取应用根的物理路径。

String[] files = Directory.GetFiles(Server.MapPath("~"), @"*", SearchOption.AllDirectories);