C# 在 Application_Start 中获取当前应用程序物理路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1495504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:06:37  来源:igfitidea点击:

Get current application physical path within Application_Start

c#asp.net

提问by Alex

I'm not able to get the current physical path within Application_Start using

我无法使用 Application_Start 获取当前的物理路径

HttpContext.Current.Request.PhysicalApplicationPath

because there is no Requestobject at that time.

因为当时没有Request对象。

How else can I get the physical path?

我还能如何获得物理路径?

采纳答案by rick schott

 protected void Application_Start(object sender, EventArgs e)
 {
     string path = Server.MapPath("/");
     //or 
     string path2 = Server.MapPath("~");
     //depends on your application needs

 }

回答by TJF

回答by maxy

use below code

使用下面的代码

server.mappath()in asp.net

server.mappath()在asp.net中

application.startuppathin c# windows application

application.startuppath在 c# windows 应用程序中

回答by DotNetUser

You can also use

你也可以使用

HttpRuntime.AppDomainAppVirtualPath

回答by Alex

There is also the static HostingEnvironment.MapPath

还有静态 HostingEnvironment.MapPath

回答by sk2185

You can use this code:

您可以使用此代码:

AppDomain.CurrentDomain.BaseDirectory

AppDomain.CurrentDomain.BaseDirectory

回答by hakan

System.AppDomain.CurrentDomain.BaseDirectory

System.AppDomain.CurrentDomain.BaseDirectory

This will give you the running directory of your application. This even works for web applications. Afterwards, you can reach your file.

这将为您提供应用程序的运行目录。这甚至适用于 Web 应用程序。之后,您可以访问您的文件。

回答by Jenna Leaf

There's, however, slight difference among all these options which

但是,所有这些选项之间略有不同,

I found out that

我发现

If you do

如果你这样做

    string URL = Server.MapPath("~");

or

或者

    string URL = Server.MapPath("/");

or

或者

    string URL = HttpRuntime.AppDomainAppPath;

your URL will display resources in your link like this:

您的 URL 将在您的链接中显示资源,如下所示:

    "file:///d:/InetPUB/HOME/Index/bin/Resources/HandlerDoc.htm"

But if you want your URL to show only virtual path not the resources location, you should do

但是如果你想让你的 URL 只显示虚拟路径而不是资源位置,你应该这样做

    string URL = HttpRuntime.AppDomainAppVirtualPath; 

then, your URL is displaying a virtual path to your resources as below

然后,您的 URL 将显示资源的虚拟路径,如下所示

    "http://HOME/Index/bin/Resources/HandlerDoc.htm"        

回答by batressc

I created a website with ASP.Net WebForms where you can see the result of using all forms mentioned in previous responses from a site in Azure.

我使用 ASP.Net WebForms 创建了一个网站,您可以在其中查看使用 Azure 中的站点的先前响应中提到的所有表单的结果。

http://wfserverpaths.azurewebsites.net/

http://wfserverpaths.azurewebsites.net/

Summary:

概括:



Server.MapPath("/") => D:\home\site\wwwroot\


Server.MapPath("~") => D:\home\site\wwwroot\


HttpRuntime.AppDomainAppPath => D:\home\site\wwwroot\


HttpRuntime.AppDomainAppVirtualPath => /


AppDomain.CurrentDomain.BaseDirectory => D:\home\site\wwwroot\


HostingEnvironment.MapPath("/") => D:\home\site\wwwroot\


HostingEnvironment.MapPath("~") => D:\home\site\wwwroot\

回答by abzarak

Best choice is using

最好的选择是使用

AppDomain.CurrentDomain.BaseDirectory

because it's in the system namespace and there is no dependencyto system.web

因为它在系统命名空间中并且不依赖system.web

this way your code will be more portable

这样你的代码会更便携