获取应用程序的根文件夹 [英] getting root folder of application

查看:120
本文介绍了获取应用程序的根文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取应用程序的根文件夹。我用下面的code,但是这给了bin文件夹,但我需要的是应用程序的根文件夹。是否有可能得到这个?

  //这是完整的目录和EXE名
        字符串fullAppName = Assembly.GetExecutingAssembly()的GetName()codeBase的。;        //这个剥去exe文件名
        字符串fullAppPath = Path.GetDirectoryName(fullAppName);


解决方案

在您的EXE的位置,是应用程序的根目录。

您可以使用字符串APPPATH = Path.GetDirectoryName(Application.ExecutablePath); 来获取应用程序的路径。

如果你想找到的解决方法是在文件夹中,我建议在开始的exe文件的位置,随后走到目录树,直到到达包含.sln文件的文件夹。也不太清楚为什么你想做到这一点,虽然。

编辑:只是想通了,你要创建一个asp.net网站。在这种情况下,你应该能够使用以下(发现这里):

 公共静态字符串MappedApplicationPath
{
   得到
   {
      字符串APP_PATH = System.Web.HttpContext.Current.Request.ApplicationPath.ToLower();
      如果(APP_PATH ==/)//网站
         APP_PATH =/;
      否则如果(!APP_PATH.EndsWith(@/))//一个虚拟
         APP_PATH + = @/;      串吧= System.Web.HttpContext.Current.Server.MapPath(APP_PATH);
      如果(!it.EndsWith(@\\))
         这+ = @\\;
      把它返还;
   }
}

I am trying to get the root folder of the application. I have used the following code, but this gives the bin folder, but what I need is the root folder of the application. Is it possible to get this?

// 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);

解决方案

The location where your exe is, is the root of the application.

You can use string appPath = Path.GetDirectoryName(Application.ExecutablePath); to get the application path.

If you want to find the folder the solution is in, i suggest starting at the exe location, then walking up the directory tree until you get to a folder containing a .sln file. Not too sure why you'd like to do this though.

EDIT: Just figured out you're creating an asp.net site. In which case you should be able to use below (found here):

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;
   }
}

这篇关于获取应用程序的根文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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