0

Given the following code:

using System;

namespace Sandbox
{
   class CommandLine
   {
      static void Main()
      {
         String[] args = Environment.GetCommandLineArgs();
         String executable = args[0];
         String[] paramList = getParamList(args);

         System.Console.WriteLine("Directory ....... {0}", Environment.CurrentDirectory);
         System.Console.WriteLine("Executable ...... {0}", args[0]);
         System.Console.WriteLine("Params .......... {0}", String.Join(", ", paramList));
      }

      private static String[] getParamList(String[] args)
      {
         String[] paramList = new String[args.Length - 1];

         for (int i = 1; i < args.Length; i++) 
         {
            int j = i - 1;
            paramList[j] = args[i];
         }

         return paramList;
      }
   }
}

... Saved as commandline.cs and csc’d to commandline.exe

I’d like to get the full path and filename of the execuatable being invoked. This code almost does it, however it’s not 100% accurate:

  • if I call it as commandline.exe foo bar baz from the same directory; all is well
  • if I call it as commandline foo bar baz from the same directory, I only get the filename sans extension; not what I want
  • if I call it as sandbox\commandline foo bar baz from the parent directory; I get the relative path and partial filename

I'm sure there's a much easier way of getting the full path and filename of the executable other than string manipulation, right?

Mike G
  • 758
  • 6
  • 18

7 Answers7

2

Application.ExecutablePath.

Note that this requires a reference to system.windows.forms.

George Duckett
  • 31,770
  • 9
  • 95
  • 162
1

That will be

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

Be aware, though, that when running your application from within Visual Studio, this will most likely return full path to a vshost file.

Community
  • 1
  • 1
Anton Gogolev
  • 113,561
  • 39
  • 200
  • 288
1

use Assembly.GetExecutingAssembly().CodeBase

Yahia
  • 69,653
  • 9
  • 115
  • 144
0

This is what we use. Seems to work from Console, WinForms, and WPF apps running from Visual Studio or standalone.

System.Reflection.Assembly entryAssembly = 
    System.Reflection.Assembly.GetEntryAssembly();
string applicationPath = entryAssembly != null ? entryAssembly.Location :
    System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
string applicationDirectory = Path.GetDirectoryName(applicationPath);
Matt Varblow
  • 7,651
  • 3
  • 34
  • 44
0
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly(‌​).Location)
animuson
  • 53,861
  • 28
  • 137
  • 147
Haris Hasan
  • 29,856
  • 10
  • 92
  • 122
0
string path = System.Windows.Forms.Application.ExecutablePath;
animuson
  • 53,861
  • 28
  • 137
  • 147
TreDubZedd
  • 2,571
  • 1
  • 15
  • 20
0
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;

Application.ExecutablePath will give you path of the application that executed your code. For instance app that called your dll. The line above provides you full path to your app.

You can read more at: HOW TO: Determine the Executing Application's Path

Vlad Bezden
  • 83,883
  • 25
  • 248
  • 179