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 bazfrom the same directory; all is well - if I call it as
commandline foo bar bazfrom the same directory, I only get the filename sans extension; not what I want - if I call it as
sandbox\commandline foo bar bazfrom 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?