Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
Assembly Class
 CodeBase Property
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
Assembly..::.CodeBase Property

Updated: November 2007

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

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

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 ();
}
J#
/** @property */
public String get_CodeBase()
JScript
public function get CodeBase () : String

Property Value

Type: System..::.String

The location of the assembly as specified originally.

Implements

_Assembly..::.CodeBase

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.

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

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, 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.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
location = filename      G1   |   Edit   |  

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;

Consider using EscapedCodeBase instead      Stefan Wenig   |   Edit   |  

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.

Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker