Get the Executable Path

Problem

You want to retrieve the path where the current executable is stored.

Solution

Read the shared StartupPath property of the System.Windows.Forms.Application class.

Discussion

The System.Windows.Forms.Application class allows you to retrieve the directory where the executable is stored, even if it isn’t a Windows application.

Console.Write("Executable is: ")
Console.WriteLine(System.Windows.Forms.Application.ExecutablePath)
Console.Write("Executable is executing in: ")
Console.WriteLine(System.Windows.Forms.Application.StartupPath)

In order to use this technique, you must reference the System. Windows.Forms namespace. Alternatively, you can simply find the current working path (using recipe Set the Current Working Path

Get Microsoft® Visual Basic® .NET Programmer's Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.