Get directory of an executing assembly in C# and Visual Basic

Continuing my series of short but useful tips and tricks, one usually needs to find the directory of an executing assembly. The simple console application below shows how to do this. The Visual Basic example is after the C# one.


using System;
using System.IO;
using System.Reflection;
namespace ConsoleApplication2
internal class Program
private static void Main(string[] args)
Console.WriteLine("Directory: " + AssemblyDirectory());
Console.WriteLine("Location: " + Assembly.GetExecutingAssembly().Location);
public static string AssemblyDirectory()
var uri = new UriBuilder(Assembly.GetExecutingAssembly().CodeBase);
return Path.GetDirectoryName(Uri.UnescapeDataString(uri.Path));

Visual Basic


Imports System.Reflection
Imports System.IO
Module Module1
Sub Main()
Console.WriteLine("Directory: " + AssemblyDirectory())
Console.WriteLine("Location: " + Assembly.GetExecutingAssembly().Location)
End Sub
Public Function AssemblyDirectory() As String
Dim uri = New UriBuilder(Assembly.GetExecutingAssembly().CodeBase)
Return Path.GetDirectoryName(System.Uri.UnescapeDataString(uri.Path))
End Function
End Module

One thought on “Get directory of an executing assembly in C# and Visual Basic

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s