.NET Framework Class Library
Assembly..::.CodeBase Property

Gets the location of the assembly as specified originally, for example, in an AssemblyName object.

Namespace:  System.Reflection
Assembly:  mscorlib (in mscorlib.dll)
Syntax

Visual Basic (Declaration)
Public Overridable ReadOnly Property CodeBase As String
Visual Basic (Usage)
Dim instance As [Assembly]
Dim value As String

value = instance.CodeBase
C#
public virtual string CodeBase { get; }
Visual C++
public:
virtual property String^ CodeBase {
    String^ get ();
}
JScript
public function get CodeBase () : String

Property Value

Type: System..::.String
The location of the assembly as specified originally.

Implements

_Assembly..::.CodeBase
Remarks

To get the absolute path to the loaded manifest-containing file, use the Assembly..::.Location property instead.

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.

Examples

The following example shows an expression that uses the CodeBase property.

Visual Basic
    Dim SampleAssembly As [Assembly]
    ' Instantiate a target object.
    Dim Integer1 As New Int32()
    Dim Type1 As Type
    ' Set the Type instance to the target class type.
    Type1 = Integer1.GetType()
    ' Instantiate an Assembly class to the assembly housing the Integer type.  
    SampleAssembly = [Assembly].GetAssembly(Integer1.GetType())
    ' Gets the location of the assembly using file: protocol.
    Console.WriteLine(("CodeBase=" + SampleAssembly.CodeBase))
End Sub
C#
Assembly SampleAssembly;
// Instantiate a target object.
Int32 Integer1 = new Int32();
Type Type1;
// Set the Type instance to the target class type.
Type1 = Integer1.GetType();
// Instantiate an Assembly class to the assembly housing the Integer type.  
SampleAssembly = Assembly.GetAssembly(Integer1.GetType());
// Gets the location of the assembly using file: protocol.
Console.WriteLine("CodeBase=" + SampleAssembly.CodeBase);
Visual C++
Assembly^ SampleAssembly;
// Instantiate a target object.
Int32 Integer1(0);
Type^ Type1;
// Set the Type instance to the target class type.
Type1 = Integer1.GetType();
// Instantiate an Assembly class to the assembly housing the Integer type.  
SampleAssembly = Assembly::GetAssembly( Integer1.GetType() );
// Gets the location of the assembly using file: protocol.
Console::WriteLine( "CodeBase= {0}", SampleAssembly->CodeBase );
JScript
var SampleAssembly : Assembly;
// Instantiate a target object.
var Integer1 : Int32 = 0;
var Type1 : Type;
// Set the Type instance to the target class type.
Type1 = Integer1.GetType();
// Instantiate an Assembly class to the assembly housing the Integer type.  
SampleAssembly = Assembly.GetAssembly(Integer1.GetType());
// Gets the location of the assembly using file: protocol.
Console.WriteLine("CodeBase=" + SampleAssembly.CodeBase);
.NET Framework Security

Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
See Also

Reference

Tags :


Community Content

G1
location = filename

Returns the fully qualified filename of the assembly, not just the folder containing it.

In particular, it prepends "file:///" as in "file:///c:/programs..." (the Universal Resource Identifier (URI) format), which cannot then be used directly in commands like XmlDocument.Load(filename). To convert to the absolute path (which can be used), parse the Uri with the available structure System.Uri, as in...

string filename = (new System.Uri(Assembly.GetExecutingAssembly.CodeBase)).AbsolutePath;


Stefan Wenig
Consider using EscapedCodeBase instead

If the path contains a hash ("#"), this property returns an invalid URI.

e.g. an assembly in c:\dir\#dir will return "file:///c:/dir/#dir", which has the following components:

schema name = "file"

hierarchical part = "///c:/dir/

fragment = "dir"

In this case, the example in the contribution above (using System.Uri) would NOT work.

Consider using Assembly.EscapedCodeBase instead, which works just fine with System.Uri. (System.Uri will unescape all other escaped characters.)

Update: MS considers this "by design", since some Apps seem to handle these invalid URLs correctly (read: it works, even if the URL is not correct). http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=333763

I believe the only way to handle those unescaped URIs correctly is to remove the "file://" prefix and leave the rest untouched. Stay away from any standard conforming URL parsers with this CodeBase, they will break in the rare scenario of paths that include hashes.


Page view tracker