• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

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

A

Andrew Falanga

#1
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
 

My Computer

J

Jeroen Mostert

#2
Andrew Falanga wrote:

> 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().

--
J.
 

My Computer

J

Jon Skeet [C# MVP]

#3
Andrew Falanga <af300wsm@xxxxxx> wrote:

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

--
Jon Skeet - <skeet@xxxxxx>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
 

My Computer

M

Mark Salsbery [MVP]

#4
"Andrew Falanga" <af300wsm@xxxxxx> wrote in message
news:27a7bd0f-b021-47b4-bafe-f106c78dd51b@xxxxxx

> 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

--
Mark Salsbery
Microsoft MVP - Visual C++



>
> Thanks,
> Andy
 

My Computer

M

Mark Salsbery [MVP]

#5
"Andrew Falanga" <af300wsm@xxxxxx> wrote in message
news:27a7bd0f-b021-47b4-bafe-f106c78dd51b@xxxxxx

> 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

--
Mark Salsbery
Microsoft MVP - Visual C++


>
> Thanks,
> Andy
 

My Computer

A

Alun Harford

#6
Jon Skeet [C# MVP] wrote:

> Andrew Falanga <af300wsm@xxxxxx> wrote:

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

My Computer

Users Who Are Viewing This Thread (Users: 1, Guests: 0)