如何获取ASP.NET应用程序的根文件夹

use*_*406 3 c# asp.net

我想获取应用程序的根文件夹.我使用了以下代码,但这给了bin文件夹,但我需要的是应用程序的根文件夹.有可能得到这个吗?

// This is the full directory and exe name
String fullAppName = Assembly.GetExecutingAssembly().GetName().CodeBase;

// This strips off the exe name
String fullAppPath = Path.GetDirectoryName(fullAppName);
Run Code Online (Sandbox Code Playgroud)

Geo*_*ett 11

exe所在的位置是应用程序的根.

您可以使用它string appPath = Path.GetDirectoryName(Application.ExecutablePath);来获取应用程序路径.

如果你想找到解决方案所在的文件夹,我建议从exe位置开始,然后沿着目录树向上走,直到找到包含.sln文件的文件夹.不太清楚为什么你想这样做.

编辑:刚想通你正在创建一个asp.net网站.在这种情况下,你应该能够在下面使用(在这里找到):

public static string MappedApplicationPath
{
   get
   {
      string APP_PATH = System.Web.HttpContext.Current.Request.ApplicationPath.ToLower();
      if(APP_PATH == "/")      //a site
         APP_PATH = "/";
      else if(!APP_PATH.EndsWith(@"/")) //a virtual
         APP_PATH += @"/";

      string it = System.Web.HttpContext.Current.Server.MapPath(APP_PATH);
      if(!it.EndsWith(@"\"))
         it += @"\";
      return it;
   }
}
Run Code Online (Sandbox Code Playgroud)