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].
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.
Is this what you want:
Assembly.GetExecutingAssembly ().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();
}
}
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.
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.
Beware of accepted answer. We've had issues with using System.AppDomain.CurrentDomain.FriendlyName
under Click-Once deployed applications. For us, this is returning "DefaultDomain", and not the original exe name.