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

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

Leave a Comment