Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

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

Reply
 
Old 08-05-2008   #1 (permalink)
Andrew Falanga


 
 

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

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 System SpecsSystem Spec
Old 08-05-2008   #2 (permalink)
Jeroen Mostert


 
 

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

Andrew Falanga wrote:
Quote:

> 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 System SpecsSystem Spec
Old 08-05-2008   #3 (permalink)
Jon Skeet [C# MVP]


 
 

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

Andrew Falanga <af300wsm@xxxxxx> wrote:
Quote:

> 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 System SpecsSystem Spec
Old 08-05-2008   #4 (permalink)
Mark Salsbery [MVP]


 
 

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


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

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


Quote:

>
> Thanks,
> Andy
My System SpecsSystem Spec
Old 08-05-2008   #5 (permalink)
Mark Salsbery [MVP]


 
 

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

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

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

Quote:

>
> Thanks,
> Andy
My System SpecsSystem Spec
Old 08-05-2008   #6 (permalink)
Alun Harford


 
 

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

Jon Skeet [C# MVP] wrote:
Quote:

> Andrew Falanga <af300wsm@xxxxxx> wrote:
Quote:

>> 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 System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Visual studio 2008 IDE application R6034 runtime error - MSDN news group moderator Please Respond. .NET General
Runtime Error (abnormal program termination) General Discussion
runtime error 2147221018 (800-401e6) No program registered toopen Vista General
application causing runtime error Vista performance & maintenance


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46