C# Tips and Tricks #6 – Find the Application Path in Console Application

There are times when you are working on a console application in C# and you might want to get the application path. You might notice that the Application.StartupPath would not be available in console application which exists in the Winforms.

How to get Application Path in a Console Application in C#?

You can use the reflection to get it easily using the property

System.Reflection.Assembly.GetExecutingAssembly().Location

Here’s a sample code demonstrating how to get the application path in console application in C#.

using System;
namespace ConsoleApp1
{
    class Program
    {   
        static void Main(string[] args)
        {

            string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
            Console.WriteLine(path);
            Console.ReadLine();
        }
    }
}

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...