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

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

问题描述

我有一个web项目,如:

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

为什么它的显示 ... \\网络\\ 而TestProject组件编译成文件夹(在其他也就是说它应该显示 ... \\网络\\ BIN 在我的想法)。

Why it's display ...\Web\ while the TestProject assembly is compiled into bin folder(in other words it should display ...\Web\bin 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 将returen .. \\网络\\ File.config (实际上该文件是复制到 .. \\网络\\ BIN \\ File.config ),一个异常将被抛出。

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

您可能会说,我应该修改 m_filePath @\\ BIN \\ File.config。但是如果我在一个控制台应用程序中使用这种方法,您认为, AppDomain.CurrentDomain.BaseDirectory + m_filePath 将返回 .. \\控制台\\ BIN \\调试\\ BIN \\ File.config (实际上该文件拷入 \\控制台\\ BIN \\调试\\ File.config ),异常会由于抛向盈余

You could say that I should modified m_filePath to @"\bin\File.config". However If I use this method in a Console app in your suggest, AppDomain.CurrentDomain.BaseDirectory + m_filePath will return ..\Console\bin\Debug\bin\File.config (actually the file was copyed into .\Console\bin\Debug\File.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,一个应用程序域再presents应用程序域,这是在应用程序执行一个孤立的环境。当你想到一个ASP.Net应用程序,其中应用程序所在的根不是bin文件夹。这是完全可能的,而且在某些情况下合理,在你的bin文件夹中没有文件,也可能根本没有bin文件夹。由于AppDomain.CurrentDomain指的是同一个对象,无论是否调用从code中的code后面或从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.

当我写code设计都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包含"箱位QUOT;在asp.net应用程序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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