Assembly.CodeBase 属性

定义

注意

Assembly.CodeBase and Assembly.EscapedCodeBase are only included for .NET Framework compatibility. Use Assembly.Location instead.

获取最初指定的程序集的位置,例如,在 AssemblyName 对象中指定的位置。Gets the location of the assembly as specified originally, for example, in an AssemblyName object.

public:
 virtual property System::String ^ CodeBase { System::String ^ get(); };
[System.Obsolete("Assembly.CodeBase and Assembly.EscapedCodeBase are only included for .NET Framework compatibility. Use Assembly.Location instead.", DiagnosticId="SYSLIB0012", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public virtual string? CodeBase { get; }
public virtual string CodeBase { get; }
public virtual string? CodeBase { get; }
[<System.Obsolete("Assembly.CodeBase and Assembly.EscapedCodeBase are only included for .NET Framework compatibility. Use Assembly.Location instead.", DiagnosticId="SYSLIB0012", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
member this.CodeBase : string
member this.CodeBase : string
Public Overridable ReadOnly Property CodeBase As String

属性值

String

程序集的位置(按照最初的指定)。The location of the assembly as specified originally.

实现

属性

示例

下面的示例使用 CodeBase 属性。The following example uses the CodeBase property.

using namespace System;
using namespace System::Reflection;

void main()
{
   // Instantiate a target object.
   int 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
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 属性。To get the absolute path to the loaded manifest-containing file, use the Assembly.Location property instead.

如果程序集是以字节数组的形式加载的,则使用 Load 采用字节数组的方法重载时,此属性返回方法调用方的位置,而不是已加载的程序集的位置。If the assembly was loaded as a byte array, using an overload of the Load method that takes an array of bytes, this property returns the location of the caller of the method, not the location of the loaded assembly.

在 .NET 5.0 和更高版本中,对于捆绑的程序集,此属性引发异常。In .NET 5.0 and later versions, for bundled assemblies, this property throws an exception.

适用于