Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取所有文件的名称_C#_Asp.net Mvc - Fatal编程技术网

C# 获取所有文件的名称

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

在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\~'.  

如何执行此操作?

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

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