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

后端 未结 4 790
天命终不由人
天命终不由人 2020-11-28 11:58

I have a web project like:

namespace Web
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, Event         


        
4条回答
  •  臣服心动
    2020-11-28 12:39

    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(@"~\");

提交回复
热议问题