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

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].

This question is related to c# command-line

The answer is


On .Net Core (or Mono), most of the answers won't apply when the binary defining the process is the runtime binary of Mono or .Net Core (dotnet) and not your actual application you're interested in. In that case, use this:

var myName = Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetEntryAssembly().Location);

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();
    }
}

IF you are looking for the full path information of your executable, the reliable way to do it is to use the following:

   var executable = System.Diagnostics.Process.GetCurrentProcess().MainModule
                       .FileName.Replace(".vshost", "");

This eliminates any issues with intermediary dlls, vshost, etc.


If you need the Program name to set up a firewall rule, use:

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

This will ensure that the name is correct both when debugging in VisualStudio and when running the app directly in windows.


Super easy, here:

Environment.CurrentDirectory + "\\" + Process.GetCurrentProcess().ProcessName

When uncertain or in doubt, run in circles, scream and shout.

class Ourself
{
    public static string OurFileName() {
        System.Reflection.Assembly _objParentAssembly;

        if (System.Reflection.Assembly.GetEntryAssembly() == null)
            _objParentAssembly = System.Reflection.Assembly.GetCallingAssembly();
        else
            _objParentAssembly = System.Reflection.Assembly.GetEntryAssembly();

        if (_objParentAssembly.CodeBase.StartsWith("http://"))
            throw new System.IO.IOException("Deployed from URL");

        if (System.IO.File.Exists(_objParentAssembly.Location))
            return _objParentAssembly.Location;
        if (System.IO.File.Exists(System.AppDomain.CurrentDomain.BaseDirectory + System.AppDomain.CurrentDomain.FriendlyName))
            return System.AppDomain.CurrentDomain.BaseDirectory + System.AppDomain.CurrentDomain.FriendlyName;
        if (System.IO.File.Exists(System.Reflection.Assembly.GetExecutingAssembly().Location))
            return System.Reflection.Assembly.GetExecutingAssembly().Location;

        throw new System.IO.IOException("Assembly not found");
    }
}

I can't claim to have tested each option, but it doesn't do anything stupid like returning the vhost during debugging sessions.


System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name;

will give you FileName of your app like; "MyApplication.exe"


This is the code which worked for me:

string fullName = Assembly.GetEntryAssembly().Location;
string myName = Path.GetFileNameWithoutExtension(fullName);

All the examples above gave me the processName with vshost or the running dll name.


  • 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.

You can use Environment.GetCommandLineArgs() to obtain the arguments and Environment.CommandLine to obtain the actual command line as entered.

Also, you can use Assembly.GetEntryAssembly() or Process.GetCurrentProcess().

However, when debugging, you should be careful as this final example may give your debugger's executable name (depending on how you attach the debugger) rather than your executable, as may the other examples.


Couple more options:

  • System.Reflection.Assembly.GetExecutingAssembly().GetName().Name
  • Path.GetFileName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase

To get the path and the name

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


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

Path.GetFileNameWithoutExtension(AppDomain.CurrentDomain.FriendlyName);

Why nobody suggested this, its simple.

Path.GetFileName(Application.ExecutablePath)

System.AppDomain.CurrentDomain.FriendlyName - Returns the filename with extension (e.g. MyApp.exe).

System.Diagnostics.Process.GetCurrentProcess().ProcessName - Returns the filename without extension (e.g. MyApp).

System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName - Returns the full path and filename (e.g. C:\Examples\Processes\MyApp.exe). You could then pass this into System.IO.Path.GetFileName() or System.IO.Path.GetFileNameWithoutExtension() to achieve the same results as the above.


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.


This should suffice:

Environment.GetCommandLineArgs()[0];

For windows apps (forms and console) I use this:

Add a reference to System.Windows.Forms in VS then:

using System.Windows.Forms;
namespace whatever
{
    class Program
    {
        static string ApplicationName = Application.ProductName.ToString();
        static void Main(string[] args)
        {
            ........
        }
    }
}

This works correctly for me whether I am running the actual executable or debugging within VS.

Note that it returns the application name without the extension.

John


Is this what you want:

Assembly.GetExecutingAssembly ().Location