如何获得ASP.NET服务器应用程序的根目录? [英] How do I get the root directory of my ASP.NET server application?

查看:140
本文介绍了如何获得ASP.NET服务器应用程序的根目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的ASP.NET应用程序的启动代码(即无请求)中,我需要获取到我的应用程序根目录的路径.我需要用它来打开根目录下某个文件夹中的文件.

In the start-up code (ie no request) of my ASP.NET application I need to get the path to the root of my app. I need this to open a file I have in a folder off the root directory.

我怎么能得到这个?

推荐答案

Server.MapPath("~"); 

将获得当前应用程序的根目录,作为磁盘上的路径.例如C:\inetpub\...

Will get you the root directory of the current application, as a path on the disk. E.g., C:\inetpub\...

请注意,~字符也可以用作ASP.NET控件中Web路径的一部分,它将填充应用程序的URL.

Note that the ~ character can be used as part of web paths in ASP.NET controls as well, it'll fill in the URL to your application.

如果您的类没有Server属性,则可以使用静态

If your class doesn't have Server property, you can use static

HttpContext.Current.Server.MapPath("~")

这篇关于如何获得ASP.NET服务器应用程序的根目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆