指定程序集的位置Specifying an Assembly's Location

可以通过两种方法来指定程序集的位置:There are two ways to specify an assembly's location:

你还可以使用.NET Framework 配置工具(mscorcfg.msc)来指定程序集位置或指定公共语言运行时用于探测程序集的位置。You can also use the .NET Framework Configuration Tool (Mscorcfg.msc) to specify assembly locations or specify locations for the common language runtime to probe for assemblies.

<使用 codeBase > 元素Using the <codeBase> Element

只能在计算机配置或同时重定向程序集版本的发行者策略文件中使用 <codeBase > 元素。You can use the <codeBase> element only in machine configuration or publisher policy files that also redirect the assembly version. 当运行时确定要使用的程序集版本时,它将从确定版本的文件应用基本代码设置。When the runtime determines which assembly version to use, it applies the code base setting from the file that determines the version. 如果未指定任何代码库,则运行时以正常方式探测程序集。If no code base is indicated, the runtime probes for the assembly in the normal way. 有关详细信息,请参阅运行时如何定位程序集For details, see How the Runtime Locates Assemblies.

下面的示例演示如何指定程序集的位置。The following example shows how to specify an assembly's location.

<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>  

对于所有具有强名称的程序集, version特性都是必需的,但对于不具有强名称的程序集,应忽略该特性。The version attribute is required for all strong-named assemblies but should be omitted for assemblies that are not strong-named. <Base> 元素需要 href 属性。The <codeBase> element requires the href attribute. 不能在 <codeBase > 元素中指定版本范围。You cannot specify version ranges in the <codeBase> element.

备注

如果要为不具有强名称的程序集提供基本代码提示,则提示必须指向应用程序基目录或应用程序基目录的子目录。If you are supplying a code base hint for an assembly that is not strong-named, the hint must point to the application base or a subdirectory of the application base directory.

<使用探测 > 元素Using the <probing> Element

运行时通过探测来查找没有代码库的程序集。The runtime locates assemblies that do not have a code base by probing. 有关探测的详细信息,请参阅运行时如何定位程序集For more information about probing, see How the Runtime Locates Assemblies.

您可以使用应用程序配置文件中的 <探测 >元素指定运行时在查找程序集时应搜索的子目录。You can use the <probing> element in the application configuration file to specify subdirectories the runtime should search when locating an assembly. 下面的示例演示如何指定运行时应搜索的目录。The following example shows how to specify directories the runtime should search.

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

PrivatePath属性包含运行时应搜索程序集的目录。The privatePath attribute contains the directories that the runtime should search for assemblies. 如果应用程序位于 C:\Program Files\MyApp,则运行时将查找未在 C:\Program Files\MyApp\Bin、C:\Program Files\MyApp\Bin2\Subbin 和 C:\Program Files\MyApp\Bin3. 中指定基本代码的程序集。If the application is located at C:\Program Files\MyApp, the runtime will look for assemblies that do not specify a code base in C:\Program Files\MyApp\Bin, C:\Program Files\MyApp\Bin2\Subbin, and C:\Program Files\MyApp\Bin3. privatePath中指定的目录必须是应用程序基目录的子目录。The directories specified in privatePath must be subdirectories of the application base directory.

请参阅See also