指定程序集的位置

注释

本文特定于 .NET Framework。 它不适用于 .NET 的较新版本实现,包括 .NET 6 及更高版本。

可以通过两种方法来指定程序集的位置:

还可以使用 .NET Framework 配置工具(Mscorcfg.msc) 指定程序集位置或指定公共语言运行时探测程序集的位置。

使用 <codeBase> 元素

只有在计算机配置文件或也重定向程序集版本的发行者策略文件中,才可以使用 <codeBase> 元素。 当运行时确定要使用的程序集版本时,它将从确定版本的文件应用基本代码设置。 如果未指示任何基本代码,运行时会以正常方式探测程序集。 有关详细信息,请参阅 运行时如何定位程序集

以下示例演示如何指定程序集的位置。

<configuration>
   <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
       <dependentAssembly>
         <assemblyIdentity name="myAssembly"
                           publicKeyToken="32ab4ba45e0a69a1"
                           culture="en-us" />
         <codeBase version="2.0.0.0"
                   href="http://www.litwareinc.com/myAssembly.dll"/>
       </dependentAssembly>
      </assemblyBinding>
   </runtime>
</configuration>

所有强命名程序集都需要 版本 属性,但对于非强名称的程序集,应省略该属性。 <codeBase> 元素需要 href 属性。 不能在 <codeBase> 元素中指定版本范围。

注释

如果要为非强名称的程序集提供代码基提示,则提示必须指向应用程序基目录或应用程序基目录的子目录。

使用 <probing> 元素

运行时通过探测查找没有代码库的程序集。 有关探测的详细信息,请参阅 运行时如何定位程序集

可以使用应用程序配置文件中的探测<元素来指定运行时在查找程序集时应搜索的子目录。> 以下示例演示如何指定运行时应搜索的目录。

<configuration>
   <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
         <probing privatePath="bin;bin2\subbin;bin3"/>
      </assemblyBinding>
   </runtime>
</configuration>

privatePath 属性包含运行时应搜索程序集的目录。 如果应用程序位于 C:\Program Files\MyApp,运行时将查找未在 C:\Program Files\MyApp\Bin、C:\Program Files\MyApp\Bin 和 C:\Program Files\MyApp\Bin2\Subbin 和 C:\Program Files\MyApp\Bin3 中指定代码库的程序集。 privatePath 中指定的目录必须是应用程序基目录的子目录。

另请参阅