How to add folder to assembly search path at runtime in .NET?

前端 未结 8 2192
慢半拍i
慢半拍i 2020-11-22 07:08

My DLLs are loaded by a third-party application, which we can not customize. My assemblies have to be located in their own folder. I can not put them into GAC (my applicatio

8条回答
  •  眼角桃花
    2020-11-22 07:47

    I came here from another (marked duplicate) question about adding the probing tag to the App.Config file.

    I want to add a sidenote to this - Visual studio had already generated an App.config file, however adding the probing tag to the pregenerated runtime tag did not work! you need a seperate runtime tag with the probing tag included. In short, your App.Config should look like this:

    
    
         
            
        
      
        
          
            
            
          
        
      
    
      
      
        
          
        
      
    
    

    This took some time to figure out so I am posting it here. Also credits to The PrettyBin NuGet Package. It is a package that moves the dlls automatically. I liked a more manual approach so I did not use it.

    Also - here is a post build script that copies all .dll/.xml/.pdb to /Lib. This unclutters the /debug (or /release) folder, what I think people try to achieve.

    :: Moves files to a subdirectory, to unclutter the application folder
    :: Note that the new subdirectory should be probed so the dlls can be found.
    SET path=$(TargetDir)\lib
    if not exist "%path%" mkdir "%path%"
    del /S /Q "%path%"
    move /Y $(TargetDir)*.dll "%path%"
    move /Y $(TargetDir)*.xml "%path%"
    move /Y $(TargetDir)*.pdb "%path%"
    

提交回复
热议问题