Why doesn't AppDomain.CurrentDomain.BaseDirectory contain a β€œbin” in an asp.net application?

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 PathTest.GetBasePath() method is defined in another project, for example:

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

Why does it display ...\Web\ , while the TestProject assembly is compiled to the bin folder (in other words, it should display ...\Web\bin in my mind).

Now I am having problems if I modified the method:

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

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

We can say that I have to change m_filePath to @"\bin\File.config" . However, if I use this method in the Console application in your suggest, AppDomain.CurrentDomain.BaseDirectory + m_filePath will return ..\Console\bin\Debug\bin\File.config (in fact, the file was copied to .\Console\bin\Debug\File.config ), an exception will be thrown due to bin excess.

In other words, in the AppDomain.CurrentDomain.BaseDirectory web application, this is a different path to which the file is copied (missing /bin ), but in the console application it is the same path.
Can anybody help me?

+19
c # .net-assembly
Dec 29 2018-11-12T00:
source share
4 answers

In MSDN, the application domain "Represents the application domain, which is the sandbox where applications run." When you think of an ASP.Net application, the root directory in which the application is located is not a bin folder. It is possible, and in some cases reasonable, that you do not have files in the bin folder and, possibly, no bin folder. Since AppDomain.CurrentDomain refers to the same object, regardless of whether you call the code from the code behind or from the dll library in the bin folder, you will end up with the root path to the website.

When I wrote code designed to work in both asp.net and windows applications, I usually 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 (unchecked) option would be to use:

 public static string GetBasePath() { return System.Reflection.Assembly.GetExecutingAssembly().Location; } 
+28
Dec 29 2018-11-12T00:
source share

If you use AppDomain.CurrentDomain.SetupInformation.PrivateBinPath instead of BaseDirectory , then you should get the correct path.

+14
Dec 29 '11 at 15:40
source share

If you need a solution that works for WinForms and web applications

  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 for binary locations

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

+9
Nov 12 '15 at 15:25
source share

When ASP.net creates your site, it displays assembly assemblies in its own special place for them. So the way is so weird.

For applications hosted on asp.net, you can use:

 string path = HttpContext.Current.Server.MapPath("~/App_Data/somedata.xml"); 
+2
Dec 29 '11 at 20:51
source share



All Articles