How to get the location of the DLL currently executing?

后端 未结 5 1525
情话喂你
情话喂你 2020-11-29 04:38

I have a config file that I need to load as part of the execution of a dll I am writing.

The problem I am having is that the place I put the dll and config file is n

相关标签:
5条回答
  • 2020-11-29 04:44

    In my case (dealing with my assemblies loaded [as file] into Outlook):

    typeof(OneOfMyTypes).Assembly.CodeBase
    

    Note the use of CodeBase (not Location) on the Assembly. Others have pointed out alternative methods of locating the assembly.

    0 讨论(0)
  • 2020-11-29 04:47

    You are looking for System.Reflection.Assembly.GetExecutingAssembly()

    string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
    string xmlFileName = Path.Combine(assemblyFolder,"AggregatorItems.xml");
    

    Note:

    The .Location property returns the location of the currently running DLL file.

    Under some conditions the DLL is shadow copied before execution, and the .Location property will return the path of the copy. If you want the path of the original DLL, use the Assembly.GetExecutingAssembly().CodeBase property instead.

    .CodeBase contains a prefix (file:\), which you may need to remove.

    0 讨论(0)
  • 2020-11-29 04:49
    System.Reflection.Assembly.GetExecutingAssembly().Location
    
    0 讨论(0)
  • 2020-11-29 05:05

    Reflection is your friend, as has been pointed out. But you need to use the correct method;

    Assembly.GetEntryAssembly()     //gives you the entrypoint assembly for the process.
    Assembly.GetCallingAssembly()   // gives you the assembly from which the current method was called.
    Assembly.GetExecutingAssembly() // gives you the assembly in which the currently executing code is defined
    Assembly.GetAssembly( Type t )  // gives you the assembly in which the specified type is defined.
    
    0 讨论(0)
  • 2020-11-29 05:06

    If you're working with an asp.net application and you want to locate assemblies when using the debugger, they are usually put into some temp directory. I wrote the this method to help with that scenario.

    private string[] GetAssembly(string[] assemblyNames)
    {
        string [] locations = new string[assemblyNames.Length];
    
    
        for (int loop = 0; loop <= assemblyNames.Length - 1; loop++)       
        {
             locations[loop] = AppDomain.CurrentDomain.GetAssemblies().Where(a => !a.IsDynamic && a.ManifestModule.Name == assemblyNames[loop]).Select(a => a.Location).FirstOrDefault();
        }
        return locations;
    }
    

    For more details see this blog post http://nodogmablog.bryanhogan.net/2015/05/finding-the-location-of-a-running-assembly-in-net/

    If you can't change the source code, or redeploy, but you can examine the running processes on the computer use Process Explorer. I written a detailed description here.

    It will list all executing dlls on the system, you may need to determine the process id of your running application, but that is usually not too difficult.

    I've written a full description of how do this for a dll inside IIS - http://nodogmablog.bryanhogan.net/2016/09/locating-and-checking-an-executing-dll-on-a-running-web-server/

    0 讨论(0)
提交回复
热议问题