2011-12-29 75 views
20

我有这样一个网络项目:为什么AppDomain.CurrentDomain.BaseDirectory在asp.net应用程序中不包含“bin”?

namespace Web 
{ 
    public partial class _Default : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      lbResult.Text = PathTest.GetBasePath(); 
     } 
    } 
} 

的方法PathTest.GetBasePath()在其他项目的定义如下:

namespace TestProject 
{ 
    public class PathTest 
    { 
     public static string GetBasePath() 
     { 
      return AppDomain.CurrentDomain.BaseDirectory; 
     } 
    } 
} 

为什么它的显示...\Web\而TestProject组件编译成bin文件夹(换言之它应该在我的想法中显示...\Web\bin)。

现在我得到了一个麻烦,如果我修改方法为:

namespace TestProject 
{ 
    public class FileReader 
    { 
     private const string m_filePath = @"\File.config"; 
     public static string Read() 
     { 
      FileStream fs = null; 
      fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + m_filePath,FileMode.Open, FileAccess.Read); 
      StreamReader reader = new StreamReader(fs); 
      return reader.ReadToEnd(); 
     } 
    } 
} 

File.config在TestProject创建。现在AppDomain.CurrentDomain.BaseDirectory + m_filePath将修复..\Web\File.config(实际上文件被复制到..\Web\bin\File.config),将会抛出异常。

你可以说我应该修改m_filePath@"\bin\File.config"。但是,如果我在您的建议中在控制台应用程序中使用此方法,AppDomain.CurrentDomain.BaseDirectory + m_filePath将返回..\Console\bin\Debug\bin\File.config(实际上文件被复制到.\Console\bin\Debug\File.config),由于盈余bin将引发异常。

换句话说,在网络应用程序中,AppDomain.CurrentDomain.BaseDirectory是将文件复制到(缺少/bin)的不同路径,但在控制台应用程序中它是相同的一个路径。
任何人都可以帮助我吗?

+0

Web应用程序的基础是包含ASPX页面的Web根目录。 bin文件夹只是根的一个子文件夹。 – Tejs 2011-12-29 15:39:37

+0

唷!我以为我疯了!我有同样的问题... – ECC 2015-10-08 12:53:47

回答

28

根据MSDN,应用程序域“表示应用程序域,这是应用程序执行的独立环境。”当您考虑ASP.Net应用程序时,应用程序所在的根目录不是bin文件夹。在bin文件夹中没有文件,并且可能根本没有bin文件夹是完全可能的,并且在某些情况下是合理的。由于AppDomain.CurrentDomain引用相同的对象,无论您是从后面的代码还是从bin文件夹中的dll调用代码,都将以网站的根路径结束。

当我写的设计既asp.net和Windows下运行的代码通常应用程式我创建一个看起来像这样的属性:

public static string GetBasePath()   
{  
    if(System.Web.HttpContext.Current == null) return AppDomain.CurrentDomain.BaseDirectory; 
    else return Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"bin"); 
} 

另一个(未经测试)选项将被使用:

public static string GetBasePath()   
{  
    return System.Reflection.Assembly.GetExecutingAssembly().Location; 
} 
+0

一个很好的解决方法,但它不回答为什么....也许没有好的答案。 – kenny 2011-12-29 15:59:58

+0

@kenny - 用一点点信息更新 – Peter 2011-12-29 16:20:15

+1

您的回答非常明确且乐于助人,非常感谢! – 2011-12-30 01:17:57

14

如果您使用AppDomain.CurrentDomain.SetupInformation.PrivateBinPath而不是BaseDirectory,那么您应该得到正确的路径。

+1

注意,根据MSDN PrivatBinPath可能是一个comman分隔字符串的文件夹。 http://msdn.microsoft.com/en-us/library/system.appdomainsetup.privatebinpath.aspx – Peter 2011-12-29 15:50:35

+0

+1这是迄今为止最好的答案@Peter FWIW在实践中它似乎并不符合我的观察。尚未准备好将其称为完整的文档错误,但 – 2013-07-02 10:41:56

+0

非常感谢 – Neo 2015-07-01 10:21:18

2

当ASP.net构建您的站点时,它会在它的特殊位置输出构建程序集。因此,以这种方式获得路径很奇怪。

对于asp。净托管的应用程序,你可以使用:

string path = HttpContext.Current.Server.MapPath("~/App_Data/somedata.xml"); 
9

如果您想,对于WinForms和Web应用程序

public string ApplicationPath 
    { 
     get 
     { 
      if (String.IsNullOrEmpty(AppDomain.CurrentDomain.RelativeSearchPath)) 
      { 
       return AppDomain.CurrentDomain.BaseDirectory; //exe folder for WinForms, Consoles, Windows Services 
      } 
      else 
      { 
       return AppDomain.CurrentDomain.RelativeSearchPath; //bin folder for Web Apps 
      } 
     } 
    } 

上述解决方案的代码片断有效的解决方案是二进制文件的位置

AppDomain.CurrentDomain.BaseDirectory仍然是Web Apps的有效路径,它只是一个根文件夹,其中web.configGlobal.asaxServer.MapPath(@"~\");相同