How does one get the application name of a C# program at runtime

  • Thread starter Thread starter Andrew Falanga
  • Start date Start date
A

Andrew Falanga

Hi,

How do I get the application name during run time. In C or C++ I use
argv[0], but in C# args[0] contains the first argument to the program
not the program name. So, what is the magic to getting this?

Thanks,
Andy
 
Andrew said:
How do I get the application name during run time. In C or C++ I use
argv[0], but in C# args[0] contains the first argument to the program
not the program name. So, what is the magic to getting this?
Environment.GetCommandLineArgs().
 
Andrew Falanga said:
How do I get the application name during run time. In C or C++ I use
argv[0], but in C# args[0] contains the first argument to the program
not the program name. So, what is the magic to getting this?

You can use Application.ExecutablePath - it's in System.Windows.Forms,
but it works for console apps too.
 
Andrew Falanga said:
Hi,

How do I get the application name during run time. In C or C++ I use
argv[0], but in C# args[0] contains the first argument to the program
not the program name. So, what is the magic to getting this?


There may be an easier way, but this works:

String thismodulefilenamestr =
System.IO.Path.GetFileName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);

Mark
 
Andrew Falanga said:
Hi,

How do I get the application name during run time. In C or C++ I use
argv[0], but in C# args[0] contains the first argument to the program
not the program name. So, what is the magic to getting this?


Here's another one

String thismodulefilenamestr =
System.IO.Path.GetFileName(System.Reflection.Assembly.GetExecutingAssembly().Location);

Mark
 
Jon said:
Andrew Falanga said:
How do I get the application name during run time. In C or C++ I use
argv[0], but in C# args[0] contains the first argument to the program
not the program name. So, what is the magic to getting this?

You can use Application.ExecutablePath - it's in System.Windows.Forms,
but it works for console apps too.

Or if you really want, you can use:

new Uri(Assembly.GetEntryAssembly().CodeBase).LocalPath

(Application.ExecutablePath is much easier though)

Alun Harford
 
Back
Top