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

C#Command Line

C# Problem Overview


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

C# Solutions


Solution 1 - C#

System.AppDomain.CurrentDomain.FriendlyName

Solution 2 - C#

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.

Solution 3 - C#

This should suffice:

Environment.GetCommandLineArgs()[0];

Solution 4 - C#

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

Solution 5 - C#

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.

Solution 6 - C#

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.

Solution 7 - C#

Why nobody suggested this, its simple.

Path.GetFileName(Application.ExecutablePath)

Solution 8 - C#

Couple more options:

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

Solution 9 - C#

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

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

Solution 10 - C#

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.

Solution 11 - C#

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

Solution 12 - C#

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.

Solution 13 - C#

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.

Solution 14 - C#

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.

Solution 15 - C#

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

Path.GetFileNameWithoutExtension(AppDomain.CurrentDomain.FriendlyName);

Solution 16 - C#

Is this what you want:

Assembly.GetExecutingAssembly ().Location

Solution 17 - C#

Super easy, here:

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

Solution 18 - C#

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

Solution 19 - C#

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

Solution 20 - C#

If you are publishing a single file application in .NET 6.0 or above, you can use Environment.ProcessPath

Solution 21 - C#

To get the path and the name

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJoakimView Question on Stackoverflow
Solution 1 - C#Steven A. LoweView Answer on Stackoverflow
Solution 2 - C#Lee GrissomView Answer on Stackoverflow
Solution 3 - C#James BView Answer on Stackoverflow
Solution 4 - C#Aaron DanielsView Answer on Stackoverflow
Solution 5 - C#Tal SegalView Answer on Stackoverflow
Solution 6 - C#Andrew HareView Answer on Stackoverflow
Solution 7 - C#xmenView Answer on Stackoverflow
Solution 8 - C#JohnBView Answer on Stackoverflow
Solution 9 - C#Teoman shipahiView Answer on Stackoverflow
Solution 10 - C#Mark UebelView Answer on Stackoverflow
Solution 11 - C#langpavelView Answer on Stackoverflow
Solution 12 - C#OrwellophileView Answer on Stackoverflow
Solution 13 - C#Jeff YatesView Answer on Stackoverflow
Solution 14 - C#theMayerView Answer on Stackoverflow
Solution 15 - C#RJNView Answer on Stackoverflow
Solution 16 - C#Frederik GheyselsView Answer on Stackoverflow
Solution 17 - C#Delusional GhostView Answer on Stackoverflow
Solution 18 - C#TobiasView Answer on Stackoverflow
Solution 19 - C#JohnView Answer on Stackoverflow
Solution 20 - C#dougcunhaView Answer on Stackoverflow
Solution 21 - C#Ewerton DutraView Answer on Stackoverflow