配置参照DLL文件路径的方法

   众所周知,.NET程序所参照的DLL按照所有的文件路径划分,可分为两类:

  • 全局DLL(注册在GAC中)
  • 私有DLL(Win程序和exe文件在同一文件夹下,Web程序在顶层web.config同级的Bin文件夹下)

  对于私有的DLL,当其数目众多时,由于没有分类,会显得比较凌乱;如果将其分类,分别放到对应的子目录,则会清爽许多.

  问题是,如何解决应用程序对DLL的查找路径问题呢?答案是在配置文件中添加如下配置项:

DLL路径配置
<?xml version="1.0" ?>
<configuration
    
xmlns:asm="urn:schemas-microsoft-com:asm.v1">
  
<runtime>
    
<asm:assemblyBinding>
      
<asm:probing privatePath="shared;common"  />
    
</asm:assemblyBinding>
  
</runtime>
</configuration>

  其中privatePath是相对于 config文件的相对路径,多个文件夹以分号(;)分隔.

  举微软的Enterprise Library 4.1作为例子,其默认为:

  调整后为:

 

 

附:config文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    
<runtime>
        
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            
<probing privatePath="Caching;Common;Configuration;Data;ExceptionHandling;Logging;PolicyInjection;Security;Unity;Validation"  />    
            
<dependentAssembly>
                
<assemblyIdentity name="Microsoft.Data.ConnectionUI"
                    publicKeyToken
="b03f5f7f11d50a3a"
                    culture
="neutral" />
                
<codeBase version="8.0.0.0"
          href
="file:///C:/Program%20Files/Microsoft%20Visual%20Studio%208/Common7/IDE/Microsoft.Data.ConnectionUI.dll" />
            
</dependentAssembly>
            
            
<dependentAssembly>
                
<assemblyIdentity name="Microsoft.Data.ConnectionUI.Dialog"
                    publicKeyToken
="b03f5f7f11d50a3a"
                    culture
="neutral" />
                
<codeBase version="8.0.0.0"
          href
="file:///C:/Program%20Files/Microsoft%20Visual%20Studio%208/Common7/IDE/Microsoft.Data.ConnectionUI.Dialog.dll" />
            
</dependentAssembly>
            
        
</assemblyBinding>
    
</runtime>
</configuration>

 

  更高级的应用,如版本重定向,请参考:.NET CLR Components—Resolving Names to Locations

 

posted @ 2010-04-29 16:48  Rickey Hu  阅读(3511)  评论(1编辑  收藏  举报