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].
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].
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().NamePath.GetFileName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBaseSystem.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.
This works if you need only the application name without extension:
Path.GetFileNameWithoutExtension(AppDomain.CurrentDomain.FriendlyName);
If you are publishing a single file application in .NET 6.0 or above, you can use Environment.ProcessPath
To get the path and the name
System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName