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.
在我的ASP的启动代码(即没有请求)。NET应用程序,我需要找到我的应用程序的根路径。我需要这个来打开我在根目录下的文件夹中的一个文件。
How can I get this?
我怎么能得到这个?
4 个解决方案
#1
51
Server.MapPath("~");
Will get you the root directory of the current application, as a path on the disk. E.g., C:\inetpub\...
将获取当前应用程序的根目录,作为磁盘上的路径。例如,C:\ inetpub \…
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.
注意~字符可以用作ASP中的web路径的一部分。NET控件,它将填入到您的应用程序的URL。
If your class doesn't have Server property, you can use static
如果您的类没有服务器属性,您可以使用静态
HttpContext.Current.Server.MapPath("~")
#2
33
HttpRuntime.AppDomainAppPath is useful if you don't have a HttpContext
available.
HttpRuntime。如果没有可用的HttpContext, AppDomainAppPath是有用的。
For example, a low-level library method to get a path relative to the current application, and it has to work whether it is a web app or not:
例如,一个低级库方法来获取与当前应用程序相关的路径,无论它是否是web应用程序,它都必须工作:
private static string GetDataFilePath() => HttpRuntime.AppDomainAppVirtualPath != null ?
Path.Combine(HttpRuntime.AppDomainAppPath, "App_Data") :
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
#3
4
You can get this from Server.MapPath method.
你可以从服务器上得到这个。MapPath方法。
Here is the MSDN Link: http://msdn.microsoft.com/en-us/library/ms524632(v=vs.90).aspx
这里是MSDN链接:http://msdn.microsoft.com/en-us/library/ms524632(v= vs90).aspx。
#4
3
Another possibility is AppDomain.CurrentDomain.BaseDirectory
另一种可能性是AppDomain.CurrentDomain.BaseDirectory
Some additional ways: Different ways of getting Path
一些附加的方法:不同的路径获取方法
#1
51
Server.MapPath("~");
Will get you the root directory of the current application, as a path on the disk. E.g., C:\inetpub\...
将获取当前应用程序的根目录,作为磁盘上的路径。例如,C:\ inetpub \…
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.
注意~字符可以用作ASP中的web路径的一部分。NET控件,它将填入到您的应用程序的URL。
If your class doesn't have Server property, you can use static
如果您的类没有服务器属性,您可以使用静态
HttpContext.Current.Server.MapPath("~")
#2
33
HttpRuntime.AppDomainAppPath is useful if you don't have a HttpContext
available.
HttpRuntime。如果没有可用的HttpContext, AppDomainAppPath是有用的。
For example, a low-level library method to get a path relative to the current application, and it has to work whether it is a web app or not:
例如,一个低级库方法来获取与当前应用程序相关的路径,无论它是否是web应用程序,它都必须工作:
private static string GetDataFilePath() => HttpRuntime.AppDomainAppVirtualPath != null ?
Path.Combine(HttpRuntime.AppDomainAppPath, "App_Data") :
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
#3
4
You can get this from Server.MapPath method.
你可以从服务器上得到这个。MapPath方法。
Here is the MSDN Link: http://msdn.microsoft.com/en-us/library/ms524632(v=vs.90).aspx
这里是MSDN链接:http://msdn.microsoft.com/en-us/library/ms524632(v= vs90).aspx。
#4
3
Another possibility is AppDomain.CurrentDomain.BaseDirectory
另一种可能性是AppDomain.CurrentDomain.BaseDirectory
Some additional ways: Different ways of getting Path
一些附加的方法:不同的路径获取方法