The path for the executable file that started the application. This path will be different depending on whether the Windows Forms application is deployed using ClickOnce. ClickOnce applications are stored in a per-user application cache in the C: Documents and Settings username directory. In VB.NET you need to use the Application object and its ExecutablePath property to get the path for the executable file that started the application, including the executable name: Dim aPath As String = Application.ExecutablePath. I'm nearly ready to distribute my vb.net application. I have several picturebox files which are loaded currently from c:/temp. I need to change this directory to one that will be OK to use when the user installs it to their PC. My question is how can I do this? Is there a way to get the installation path, then use that within the code as a. Apr 18, 2009  Just a quick 'Thank You' as this answered my question/solved my problem. However, to provide a bit further info, in VB.NET 2002 at least (net 1.1) I found that Application.ExecutablePath was not useful, as it had not only the path, but included the executable file name as well, which I would have had to strip, however, the other suggestion: Application.StartupPath worked perfectly.

  1. Vb.net Application Path Variable
  2. Vb.net App.path
  3. Vb.net Application Startup Path
  4. Vb.net Current Application Path
Active1 month ago

How do I find the application's path in a console application?

In Windows Forms, I can use Application.StartupPath to find the current path, but this doesn't seem to be available in a console application.

Peter Mortensen
14.5k19 gold badges89 silver badges118 bronze badges
JSmythJSmyth
4,9603 gold badges19 silver badges18 bronze badges

26 Answers

System.Reflection.Assembly.GetExecutingAssembly().Location1

Combine that with System.IO.Path.GetDirectoryName if all you want is the directory.

1As per Mr.Mindor's comment:
System.Reflection.Assembly.GetExecutingAssembly().Location returns where the executing assembly is currently located, which may or may not be where the assembly is located when not executing. In the case of shadow copying assemblies, you will get a path in a temp directory. System.Reflection.Assembly.GetExecutingAssembly().CodeBase will return the 'permanent' path of the assembly.

Sebastian Brosch
28.5k12 gold badges47 silver badges60 bronze badges
Sam AxeSam Axe
27.9k7 gold badges45 silver badges75 bronze badges

You can use the following code to get the current application directory.

Richard Everett
34.3k50 gold badges169 silver badges265 bronze badges
Sarathy

You have two options for finding the directory of the application, which you chose will depend on your purpose.

shA.t
13.5k4 gold badges40 silver badges78 bronze badges
Mr.MindorMr.Mindor
3,3992 gold badges15 silver badges22 bronze badges

Probably a bit late but this is worth a mention:

Or more correctly to get just the directory path:

Edit:

Quite a few people have pointed out that GetCommandLineArgs is not guaranteed to return the program name. See The first word on the command line is the program name only by convention. The article does state that 'Although extremely few Windows programs use this quirk (I am not aware of any myself)'. So it is possible to 'spoof' GetCommandLineArgs, but we are talking about a console application. Console apps are usually quick and dirty. So this fits in with my KISS philosophy.

shA.t
13.5k4 gold badges40 silver badges78 bronze badges
Steve McSteve Mc

For anyone interested in asp.net web apps. Here are my results of 3 different methods

result

the app is physically running from 'C:inetpubSBSPortal_staging', so the first solution is definitely not appropriate for web apps.

rocketsarefastrocketsarefast
3,3721 gold badge17 silver badges16 bronze badges

The answer above was 90% of what I needed, but returned a Uri instead of a regular path for me.

As explained in the MSDN forums post, How to convert URI path to normal filepath?, I used the following:

fizzledfizzled
Peter Mortensen
14.5k19 gold badges89 silver badges118 bronze badges
PSU_KardiPSU_Kardi
2,1014 gold badges35 silver badges68 bronze badges
Matas Vaitkevicius
37.6k17 gold badges179 silver badges190 bronze badges
ButtShockButtShock

For Console Applications, you can try this:

Output (on my local machine):

c:usersxxxxxxxdocumentsvisual studio 2012ProjectsImageHandlerGetDirbinDebug

Or you can try (there's an additional backslash in the end):

Output:

c:usersxxxxxxxdocumentsvisual studio 2012ProjectsImageHandlerGetDirbinDebug

F.AlvesF.Alves
Trimantra Software SolutionTrimantra Software Solution
2,5981 gold badge15 silver badges32 bronze badges

If you are looking for a .NET Core compatible way, use

This was introduced in .NET Framework 4.6 and .NET Core 1.0 (and .NET Standard 1.3). See: AppContext.BaseDirectory Property.

According to this page,

This is the prefered replacement for AppDomain.CurrentDomain.BaseDirectory in .NET Core

DejanDejan
3,5211 gold badge33 silver badges69 bronze badges

I have used

when I want to find a path relative to an applications folder. This works for both ASP.Net and winform applications. It also does not require any reference to System.Web assemblies.

user2346593user2346593

You can simply add to your project references System.Windows.Forms and then use the System.Windows.Forms.Application.StartupPath as usual .

So, not need for more complicated methods or using the reflection.

fruggierofruggiero

I use this if the exe is supposed to be called by double clicking it

developer747developer747
6,25820 gold badges69 silver badges125 bronze badges
Vb Net Application Path

I mean, why not a p/invoke method?

You would use it just like the Application.StartupPath:

user3596865user3596865

Following line will give you an application path:

Above solution is working properly in following situations:

  • simple app
  • in another domain where Assembly.GetEntryAssembly() would return null
  • DLL is loaded from Embedded resources as byte array and loaded to AppDomain as Assembly.Load(byteArrayOfEmbeddedDll)
  • with Mono's mkbundle bundles (no other methods work)
Kuba Ober
74.2k10 gold badges92 silver badges215 bronze badges
user2126375user2126375

Assembly.GetEntryAssembly().Location or Assembly.GetExecutingAssembly().Location

Use in combination with System.IO.Path.GetDirectoryName() to get only the directory.

The paths from GetEntryAssembly() and GetExecutingAssembly() can be different, even though for most cases the directory will be the same.

With GetEntryAssembly() you have to be aware that this can return null if the entry module is unmanaged (ie C++ or VB6 executable). In those cases it is possible to use GetModuleFileName from the Win32 API:

HermanHerman

Will resolve the issue to refer the 3rd party reference files with installation packages.

Nirav MehtaNirav Mehta

in VB.net

works for me (Application Type: Class Library). Not sure about C#..Returns the path w/o Filename as string

Nicolas Raoul
35.7k49 gold badges171 silver badges304 bronze badges
dbadba
4291 gold badge6 silver badges15 bronze badges

None of these methods work in special cases like using a symbolic link to the exe, they will return the location of the link not the actual exe.

So can use QueryFullProcessImageName to get around that:

Net
colin lamarrecolin lamarre
daniele3004daniele3004
7,0417 gold badges44 silver badges53 bronze badges

Another solution is using relative paths pointing to the current path:

Preferences can be saved as templates for easy restoration and sharing among Special Effects users. Free special fx. Special Effects uses pixel animation to create the following 3D displays in real time: fireworks, star fields, B-spline warp, B-spline sparklers, and gravity wells. Many user preferences can be specified to customize the results, such as the number of fireworks shells, configuration of bursts, number of sparks per shell, persistence of sparks, gravity, atmospheric damping, combination of colors, and the viewing focal length.

blenderfreakyblenderfreaky

I didn't see anyone convert the LocalPath provided by .Net Core reflection into a usable System.IO path so here's my version.

This will return the full 'C:xxxxxx' formatted path to where your code is.

mark gamachemark gamache

There are many ways to get executable path, which one we should use it depends on our needs here is a link which discuss different methods.

Nasir MahmoodNasir Mahmood
1,1001 gold badge10 silver badges17 bronze badges

Here is a reliable solution that works with 32bit and 64bitYamaha psr e423 drivers. applications.

Add these references:

using System.Diagnostics;

using System.Management;

Add this method to your project:

Now use it like so:

Notice that if you know the id of the process, then this method will return the corresponding ExecutePath.

Vb.net Application Path Variable

Extra, for those interested:

..will give you an array of all the currently running processes, and..

..will give you the current process, along with their information e.g. Id, etc. and also limited control e.g. Kill, etc.*

Knickerless-NogginsKnickerless-Noggins
5,8033 gold badges47 silver badges62 bronze badges

You can create a folder name as Resources within the project using Solution Explorer,then you can paste a file within the Resources.

Vb.net App.path

Perception
70.7k11 gold badges154 silver badges177 bronze badges
Devarajan.TDevarajan.T

protected by Patrick HofmanAug 4 '14 at 13:56

Vb.net Application Startup Path

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?

Vb.net Current Application Path

Not the answer you're looking for? Browse other questions tagged c#.netconsoleconsole-application or ask your own question.

r5gnd.netlify.com – 2018