Windows Vista Forums

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

  1. #1


    Andrew Falanga Guest

    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

  2.   


  3. #2


    Jeroen Mostert Guest

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

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

  4. #3


    Jon Skeet [C# MVP] Guest

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

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

  5. #4


    Mark Salsbery [MVP] Guest

    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

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

  6. #5


    Mark Salsbery [MVP] Guest

    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

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

  7. #6


    Alun Harford Guest

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

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


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

Similar Threads
Thread Forum
32bit application compiles in 64bit mode but errors in runtime .NET General
Visual studio 2008 IDE application R6034 runtime error - MSDN news group moderator Please Respond. .NET General
Runtime Error (abnormal program termination) General Discussion
application causing runtime error Vista performance & maintenance
How to change Style at runtime in application-level Avalon