HiveBrain v1.2.0
Get Started
← Back to all entries
snippetcsharpCritical

How can I get the application's path in a .NET console application?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
nethowconsolethecanpathapplicationget

Problem

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.

Solution

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.

Context

Stack Overflow Q#837488, score: 1362

Revisions (0)

No revisions yet.