2

在我的 asp.net Web 应用程序中,我必须读取 App_Data 文件夹中的 somef 文件,但我不知道如何设置路径。

如果在网络表单中,我知道我可以使用:

Sever.mapPath("~/App_Data");

但现在我想在 App_code 中检索 .cs 文件中的路径。

所以那里没有 HttpServer 上下文。

任何的想法?

4

2 回答 2

12

请改用HostingEnvironment.MapPath

于 2012-04-18T06:17:38.470 回答
3

在 asp.net 中有 System.IO 命名空间,其中包含许多与文件相关的功能。

您可以在下面的命名空间中获得与路径和文件相关的功能

System.IO.Path
System.IO.File

您可以在 .cs 文件中获取完整的文件路径,如下所示

string strFullPath = System.IO.Path.GetFullPath(Server.MapPath("~/App_Code/Class1.cs"));    

希望这会帮助你...

于 2012-04-18T06:55:34.537 回答