简体   繁体   中英

Why AppDomain.CurrentDomain.BaseDirectory not contains "bin" in asp.net app?

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

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

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

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

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.

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 .

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?

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.

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

In case you want a solution that works for WinForms and Web Apps

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

Above solution code snippet is for binaries locations

The AppDomain.CurrentDomain.BaseDirectory is still valid path for Web Apps, it's just a root folder where the web.config and Global.asax and is same as Server.MapPath(@"~\\");

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

When ASP.net builds your site it outputs build assemblies in its special place for them. So getting path in that way is strange.

For asp.net hosted applications you can use:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM