How can I get the application's path in a .NET console application?

后端 未结 27 1409
甜味超标
甜味超标 2020-11-21 11:11

How do I find the application\'s path in a console application?

In Windows Forms, I can use Application.StartupPath to find the current path, but this d

27条回答
  •  攒了一身酷
    2020-11-21 11:55

    For anyone interested in asp.net web apps. Here are my results of 3 different methods

    protected void Application_Start(object sender, EventArgs e)
    {
      string p1 = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
      string p2 = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
      string p3 = this.Server.MapPath("");
      Console.WriteLine("p1 = " + p1);
      Console.WriteLine("p2 = " + p2);
      Console.WriteLine("p3 = " + p3);
    }
    

    result

    p1 = C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\a897dd66\ec73ff95\assembly\dl3\ff65202d\29daade3_5e84cc01
    p2 = C:\inetpub\SBSPortal_staging\
    p3 = C:\inetpub\SBSPortal_staging
    

    the app is physically running from "C:\inetpub\SBSPortal_staging", so the first solution is definitely not appropriate for web apps.

提交回复
热议问题