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

前端 未结 30 2790
小蘑菇
小蘑菇 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:52

    This should work:

    ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
    Assembly asm = Assembly.GetCallingAssembly();
    String path = Path.GetDirectoryName(new Uri(asm.EscapedCodeBase).LocalPath);
    
    string strLog4NetConfigPath = System.IO.Path.Combine(path, "log4net.config");
    

    I am using this to deploy DLL file libraries along with some configuration file (this is to use log4net from within the DLL file).

提交回复
热议问题