How do I get the path of the assembly the code is in?

前端 未结 30 2833
小蘑菇
小蘑菇 2020-11-21 16:17

Is there a way to get the path for the assembly in which the current code resides? I do not want the path of the calling assembly, just the one containing the code.

<
30条回答
  •  臣服心动
    2020-11-21 16:59

    For ASP.Net, it doesn't work. I found a better covered solution at Why AppDomain.CurrentDomain.BaseDirectory not contains "bin" in asp.net app?. It works for both Win Application and ASP.Net Web Application.

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

提交回复
热议问题