#961 – Retrieving Command Line and Path to Executable

You can easily get information about the full command used to launch your application, as well as the path to the executable containing your code.

  • Environment.CommandLine is a string containing the full command line that launched your application
  • Environment.CurrentDirectory is a string containing the full path to the current working directory (which may be different from the directory containing your application)
  • Assembly.GetExecutingAssembly().CodeBase is a string containing the full path to the executable or DLL containing the code that is currently executing

For example:

            Console.WriteLine("Your full command line was:");
            Console.WriteLine(string.Format("  {0}", Environment.CommandLine));

            Console.WriteLine("Current working directory is:");
            Console.WriteLine(string.Format("  {0}", Environment.CurrentDirectory));

            // Requires: using System.Reflection;
            Console.WriteLine("Full path to executable:");
            Console.WriteLine(string.Format("  {0}", Assembly.GetExecutingAssembly().CodeBase));

If we launch the application as follows:
961-001
We’ll see the following output:
961-002

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

Leave a comment