为什么 AppDomain.CurrentDomain.BaseDirectory 不包含“bin"?在asp.net 应用程序中? [英] Why AppDomain.CurrentDomain.BaseDirectory not contains "bin" in asp.net app?

查看:32
本文介绍了为什么 AppDomain.CurrentDomain.BaseDirectory 不包含“bin"?在asp.net 应用程序中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似的网络项目:

I have a web project like:

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

方法 PathTest.GetBasePath() 在另一个项目中定义,如:

The method PathTest.GetBasePath() is defined in another Project like:

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

为什么它显示 ...Web 而 TestProject 程序集被编译到 bin 文件夹中(换句话说,它应该显示 ...Webin 在我看来).

Why it's display ...Web while the TestProject assembly is compiled into bin folder(in other words it should display ...Webin in my thought).

现在如果我把方法修改成:

Now I got a troublesome if I modified method into:

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 将返回 ..WebFile.config (实际上文件被复制到 ..WebinFile.config),会抛出异常.

The File.config is created in TestProject. Now AppDomain.CurrentDomain.BaseDirectory + m_filePath will returen ..WebFile.config (actually the file was be copied into ..WebinFile.config), an exception will be thrown.

你可以说我应该将 m_filePath 修改为 @"inFile.config".但是,如果我在您建议的控制台应用程序中使用此方法,AppDomain.CurrentDomain.BaseDirectory + m_filePath 将返回 ..ConsoleinDebuginFile.config(实际上是拷贝到.ConsoleinDebugFile.config中),bin多余会抛出异常.

You could say that I should modified m_filePath to @"inFile.config". However If I use this method in a Console app in your suggest, AppDomain.CurrentDomain.BaseDirectory + m_filePath will return ..ConsoleinDebuginFile.config (actually the file was copyed into .ConsoleinDebugFile.config), an exception will be thrown due to surplus bin.

换句话说,在 web 应用程序中,AppDomain.CurrentDomain.BaseDirectory 是文件被复制到的不同路径(缺少 /bin),但在控制台应用程序中这是同一个路径.
有人可以帮我吗?

In other words, in web app, AppDomain.CurrentDomain.BaseDirectory is a different path where file be copyed into (lack of /bin), but in console app it's the same one path.
Any one can help me?

推荐答案

根据 MSDN,应用程序域代表应用程序域,它是应用程序执行的隔离环境."当您考虑 ASP.Net 应用程序时,应用程序所在的根目录不是 bin 文件夹.完全有可能,在某些情况下是合理的,bin 文件夹中没有文件,甚至可能根本没有 bin 文件夹.由于 AppDomain.CurrentDomain 指的是同一个对象,无论您是从后面的代码还是从 bin 文件夹中的 dll 调用代码,您最终都会得到网站的根路径.

Per MSDN, an App Domain "Represents an application domain, which is an isolated environment where applications execute." When you think about an ASP.Net application the root where the app resides is not the bin folder. It is totally possible, and in some cases reasonable, to have no files in your bin folder, and possibly no bin folder at all. Since AppDomain.CurrentDomain refers to the same object regardless of whether you call the code from code behind or from a dll in the bin folder you will end up with the root path to the web site.

当我编写了在 asp.net 和 windows 应用程序下运行的代码时,通常我会创建一个如下所示的属性:

When I've written code designed to run under both asp.net and windows apps usually I create a property that looks something like this:

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

另一个(未经测试的)选项是使用:

Another (untested) option would be to use:

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

这篇关于为什么 AppDomain.CurrentDomain.BaseDirectory 不包含“bin"?在asp.net 应用程序中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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