Assembly.CodeBase 属性

定义

获取最初指定的程序集的位置,例如,在对象 AssemblyName 中。

public:
 virtual property System::String ^ CodeBase { System::String ^ get(); };
public virtual string CodeBase { get; }
member this.CodeBase : string
Public Overridable ReadOnly Property CodeBase As String

属性值

最初指定的程序集的位置。

实现

例外

仅限 .NET Core 和 .NET 5+ :在所有情况下。

示例

以下示例使用该 CodeBase 属性。

using System;
using System.Reflection;

public class Example
{
   public static void Main()
   {
      // Instantiate a target object.
      Int32 integer1 = 1632;
      // Instantiate an Assembly class to the assembly housing the Integer type.
      Assembly systemAssembly = integer1.GetType().Assembly;
      // Get the location of the assembly using the file: protocol.
      Console.WriteLine("CodeBase = {0}", systemAssembly.CodeBase);
   }
}
// The example displays output like the following:
//    CodeBase = file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/mscorlib.dll
Imports System.Reflection

Module Example
   Public Sub Main()
      ' Instantiate a target object.
      Dim integer1 As Integer = 1632
      ' Instantiate an Assembly class to the assembly housing the Integer type.
      Dim systemAssembly As Assembly = integer1.GetType().Assembly
      ' Get the location of the assembly using the file: protocol.
      Console.WriteLine("CodeBase = {0}", systemAssembly.CodeBase)
   End Sub
End Module
' The example displays output like the following:
'   CodeBase = file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/mscorlib.dll

注解

若要获取已加载清单包含文件的绝对路径,请改用该 Assembly.Location 属性。

如果程序集作为字节数组加载,使用采用字节数组的方法的重载 Load ,则此属性返回方法调用方的位置,而不是加载的程序集的位置。

在 .NET 5 及更高版本中,对于捆绑程序集,此属性将引发异常。

适用于