Executable directory where application is running from?
Asked Answered
T

6

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!

Thermosphere answered 7/4, 2010 at 14:54 Comment(2)
dont know much about vs 2008 but when you do a dirinfo dont you get the dirinfo from where the app. is running?Giavani
Possible duplicate of Get programs path?Filberto
H
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)

Hooligan answered 7/4, 2010 at 14:58 Comment(3)
strPath contains file:\\c:\\xxxxxxSwee
This is not safe, for example when it is being used to open config file it causes System.Configuration.ConfigurationErrorsException: An error occurred loading a configuration file: URI formats are not supportedAbeyance
This link is out of date :(.Sismondi
C
41

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");
Clear answered 7/1, 2016 at 6:10 Comment(1)
System.AppDomain.CurrentDomain.BaseDirectory() Works good also in console/service application!Gabbi
H
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)

Hooligan answered 7/4, 2010 at 14:58 Comment(3)
strPath contains file:\\c:\\xxxxxxSwee
This is not safe, for example when it is being used to open config file it causes System.Configuration.ConfigurationErrorsException: An error occurred loading a configuration file: URI formats are not supportedAbeyance
This link is out of date :(.Sismondi
C
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.

Cleareyed answered 22/9, 2016 at 14:15 Comment(6)
Smallest, easiest solution, with formatting! +1, thanks.Papule
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!Cheston
@Cheston 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.Nacre
@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.CurrentDirectoryLyndes
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.Designedly
Digging old posts and found this! Thanks a lot, saved my day!Robledo
Z
14
Dim P As String = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase)
P = New Uri(P).LocalPath
Zack answered 7/9, 2014 at 9:16 Comment(0)
F
5

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

Fajardo answered 7/4, 2010 at 14:58 Comment(0)
V
0

You can write the following:

Path.Combine(Path.GetParentDirectory(GetType(MyClass).Assembly.Location), "Images\image.jpg")
Vanward answered 7/4, 2010 at 14:57 Comment(1)
There are pitfalls to this approach as the assembly does not necessarily reside in the same directory as the executing assembly.Python

© 2022 - 2024 — McMap. All rights reserved.