C# 获取所有文件的名称
在ASP.NET MVC项目中,我需要加载项目中所有文件的名称,因此我尝试:C# 获取所有文件的名称,c#,asp.net-mvc,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 Add
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\~'.
如何执行此操作?ASP.net的
~
符号描述了应用程序根目录的路径。您必须使用Server.MapPath(“~”)
来获取应用程序根目录的物理路径
String[] files = Directory.GetFiles(Server.MapPath("~"), @"*", SearchOption.AllDirectories);