Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executable directory where application is running from?

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!

like image 580
JPJedi Avatar asked Apr 07 '10 14:04

JPJedi


People also ask

Where is the executable path?

Right-click the “Start” menu shortcut for the application, and select More > Open file location. This will open a File Explorer window that points to the actual application shortcut file. Right click on that shortcut, and select “Properties.” No matter how you located the shortcut, a properties window will appear.

How do I get the current directory in VB net?

You can use WinDir variable to get the current directory. The Environment. GetEnvironmentVariable method can be used for that. Here is the code snippet that gets the current directory using VB.NET.


2 Answers

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"); 
like image 158
Derek Ziemba Avatar answered Sep 21 '22 23:09

Derek Ziemba


Dim strPath As String = System.IO.Path.GetDirectoryName( _     System.Reflection.Assembly.GetExecutingAssembly().CodeBase) 

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

like image 20
Justin Niessner Avatar answered Sep 19 '22 23:09

Justin Niessner