How do I get the name of the current executable in C#?

Viewed 318302

I want to get the name of the currently running program, that is the executable name of the program. In C/C++ you get it from args[0].

21 Answers
System.AppDomain.CurrentDomain.FriendlyName

This should suffice:

Environment.GetCommandLineArgs()[0];

System.Diagnostics.Process.GetCurrentProcess() gets the currently running process. You can use the ProcessName property to figure out the name. Below is a sample console app.

using System;
using System.Diagnostics;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(Process.GetCurrentProcess().ProcessName);
        Console.ReadLine();
    }
}

Try this:

System.Reflection.Assembly.GetExecutingAssembly()

This returns you a System.Reflection.Assembly instance that has all the data you could ever want to know about the current application. I think that the Location property might get what you are after specifically.

Couple more options:

  • System.Reflection.Assembly.GetExecutingAssembly().GetName().Name
  • Path.GetFileName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase
  • System.Reflection.Assembly.GetEntryAssembly().Location returns location of exe name if assembly is not loaded from memory.
  • System.Reflection.Assembly.GetEntryAssembly().CodeBase returns location as URL.

This works if you need only the application name without extension:

Path.GetFileNameWithoutExtension(AppDomain.CurrentDomain.FriendlyName);

Is this what you want:

Assembly.GetExecutingAssembly ().Location

To get the path and the name

System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName

Related