Get the application exe name


This came up on an internal alias recently…. Nothing earth shattering here, but I figured it might help someone googling for an answer someday…  Hey, if it did, drop me a comment and let me know about it…


 


Question:


I just want the exe name of a console application but have no idea where its at.


For example:


In C++:


#include <iostream.h>


int main(int argc, char* argv[])


{


            cout<<argv[0];   //This will give you the exe name


}


 


Answer:


In C#


     class Class1


     {


          static void Main(string[] args)


          {


              Console.WriteLine (Environment.GetCommandLineArgs()[0]);


          }


     }


 

Comments (7)

  1. Tim Pinkawa says:

    I was looking for this earlier, but couldn’t remember exactly where it was in Environment. Thanks! 🙂

  2. In WinForms it’s as easy as

    Console.WriteLine(Application.ExecutablePath);

    Please note, that one should take care about different URI schemes, because executable can be started from different code bases, there are original location and local path (cache) and so on… Think also about shadow copying.

  3. Luc Cluitmans says:

    Another version:

    System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName

  4. Johan Ericsson says:

    For C++, don’t include <iostream.h>. instead include <iostream>

    See:

    http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.4

    #include <iostream>

    int main(int argc, char* argv[])

    {

    std::cout<<argv[0]; //This will give you the exe name

    }

  5. Saurabh Jain says:

    Dont forget that each one of the solution requires different security permission and depending upon your situation, some of solution might not be suitable for you.

    Environment.GetCommandLineArgs() requires EnvironmentPermissionAccess.Read (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemEnvironmentClassGetCommandLineArgsTopic.asp).

    Application.ExecutablePath requires FileIOPermissionAccess.PathDiscovery (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformsapplicationclassexecutablepathtopic.asp).

    Process.GetCurrentProcess() (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdiagnosticsprocessclassgetcurrentprocesstopic.asp).

    is just evil. From MSDN,

    .NET Framework Security:

    SecurityPermission for calling any members of System.Diagnostic.Process with full trust. Associated enumeration: PermissionState.Unrestricted

    Also pay attention to the assemblies these methods are defined in. In particular, calling Application.ExecutablePath on a console app would result in loading of System.Windows.Form and all the referenced assemblies.

  6. Saurabh Jain says:

    Dont forget that each one of the solution requires different security permission and depending upon your situation, some of solution might not be suitable for you.

    Environment.GetCommandLineArgs() requires EnvironmentPermissionAccess.Read (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemEnvironmentClassGetCommandLineArgsTopic.asp).

    Application.ExecutablePath requires FileIOPermissionAccess.PathDiscovery (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformsapplicationclassexecutablepathtopic.asp).

    Process.GetCurrentProcess() (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdiagnosticsprocessclassgetcurrentprocesstopic.asp).

    is just evil. From MSDN,

    .NET Framework Security:

    SecurityPermission for calling any members of System.Diagnostic.Process with full trust. Associated enumeration: PermissionState.Unrestricted

    Also pay attention to the assemblies these methods are defined in. In particular, calling Application.ExecutablePath on a console app would result in loading of System.Windows.Form and all the referenced assemblies.

  7. Curtis Konelsky says:

    THANK YOU for posting this. I spent a fair bit of time trying to find this info.