30

I need to get the path (not the executable) where my application is running from:

System.AppDomain.CurrentDomain.BaseDirectory()

When I run the above statement with & "/images/image.jpg" on my local machine it works fine but when I install the application on another machine it says it cannot find the file and there is a lot of extra path information some.

I just need the directory of where the app is running. I am coding in VB.NET with Visual Studio 2008.

Thanks!

Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151
JPJedi
  • 1,498
  • 7
  • 32
  • 58
  • dont know much about vs 2008 but when you do a dirinfo dont you get the dirinfo from where the app. is running? – Grumpy Apr 07 '10 at 14:59
  • Possible duplicate of [Get programs path?](http://stackoverflow.com/questions/2216141/get-programs-path) – Muhammad Saqib Jul 10 '16 at 17:03

6 Answers6

39

This is the first post on google so I thought I'd post different ways that are available and how they compare. Unfortunately I can't figure out how to create a table here, so it's an image. The code for each is below the image using fully qualified names.

enter image description here

My.Application.Info.DirectoryPath

Environment.CurrentDirectory

System.Windows.Forms.Application.StartupPath

AppDomain.CurrentDomain.BaseDirectory

System.Reflection.Assembly.GetExecutingAssembly.Location

System.Reflection.Assembly.GetExecutingAssembly.CodeBase

New System.UriBuilder(System.Reflection.Assembly.GetExecutingAssembly.CodeBase)

Path.GetDirectoryName(Uri.UnescapeDataString((New System.UriBuilder(System.Reflection.Assembly.GetExecutingAssembly.CodeBase).Path)))

Uri.UnescapeDataString((New System.UriBuilder(System.Reflection.Assembly.GetExecutingAssembly.CodeBase).Path))

--- Edit October 18, 2021:

Sigh... None of the above work if using net5.0 or net6.0 and publishing app as single-file bundle. Best I got now is:

// This will give you the directory but not the assembly
string basedir = AppContext.BaseDirectory;
// Before you package the app as a single file bundle, you will get the dll.  
// But after you publish it, you'll get the exe. 
string pathToExecutable = Environment.GetCommandLineArgs()[0].Replace(".dll", ".exe");
Derek Ziemba
  • 2,467
  • 22
  • 22
  • 1
    System.AppDomain.CurrentDomain.BaseDirectory() Works good also in console/service application! – BioBier Sep 26 '18 at 13:29
28
Dim strPath As String = System.IO.Path.GetDirectoryName( _
    System.Reflection.Assembly.GetExecutingAssembly().CodeBase)

Taken from HOW TO: Determine the Executing Application's Path (MSDN)

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
15

I needed to know this and came here, before I remembered the Environment class.

In case anyone else had this issue, just use this: Environment.CurrentDirectory.

Example:

Dim dataDirectory As String = String.Format("{0}\Data\", Environment.CurrentDirectory)

When run from Visual Studio in debug mode yeilds:

C:\Development\solution folder\application folder\bin\debug

This is the exact behaviour I needed, and its simple and straightforward enough.

MuTiny
  • 261
  • 1
  • 7
  • 23
Jason Alls
  • 159
  • 1
  • 4
  • 3
    Smallest, easiest solution, with formatting! +1, thanks. – rdtsc Jan 19 '17 at 20:17
  • 2
    Yes, this help to make the application truly portable, by keeping the settings file in the root path of the application. Thanks for the simple solution! – vr_driver Feb 24 '18 at 14:41
  • 2
    @vr_driver This is bad. For portable the executable file's parent directory should be used, not the current directory, which can be different from the former. For example if a script running in `%windir%` calls the program using an absolute path it will look for ` %windir%\data\ ` and that is bad. – beppe9000 Sep 30 '19 at 13:42
  • @jason-alls: Your example output is not 100% correct. With your code you get `C:\Development\solution folder\application folder\bin\debug\DATA` because you add it to your value from `Environment.CurrentDirectory` – PeterCo Nov 27 '19 at 09:28
  • This is the answer that worked for me. All the Application properties were returning .Net framework -based results, this returned the actual directory my app is running in. – Cirieno Jun 18 '21 at 08:35
  • Digging old posts and found this! Thanks a lot, saved my day! – Moiyd Jun 25 '21 at 12:00
14
Dim P As String = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase)
P = New Uri(P).LocalPath
MrCalvin
  • 1,675
  • 1
  • 19
  • 27
5

You could use the static StartupPath property of the Application class.

Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151
0

You can write the following:

Path.Combine(Path.GetParentDirectory(GetType(MyClass).Assembly.Location), "Images\image.jpg")
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 1
    There are pitfalls to this approach as the assembly does not necessarily reside in the same directory as the executing assembly. – Dave Jellison Feb 15 '11 at 14:25