Tech Off Thread

5 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

"Where o' where did my arg[0] go?"

Back to Forum: Tech Off
  • User profile image
    ScanIAm

    Where o' where can it be?

    If you compile and run the code below, you will see that arg[0] no longer contains the name of the .exe that was executed (as it did in C/C++).  Am I missing something here?

    namespace ConsoleApplication4

    {

    class Class1

    {

    [STAThread]

    static void Main(string[] args)

    {

    for(int i = 0;i < args.Length;i++)

    Console.WriteLine(i + " " + args[i]);

    Console.ReadLine();

    }

    }

    }

  • User profile image
    Minh

    You're not missing anything... args now just contain arguments. Use Environment.CommandLine for the executable name. It will contain full path name, so prepare to parse (well, there's an API for that too)

  • User profile image
    amotif

    Ditto what Minh said.  C# is not C/C++ in a lot of ways.  You can still do it the crufty old way in Managed C++. Smiley

    (still using C++, since 1988)

  • User profile image
    Sven Groot

    Environment.CommandLine contains the entire command-line, including arguments, so it may or may not be what you want.

    Environment.GetCommandLineArgs[0] is the executable name, but may or may not include full path and file name extension, depending on the circumstances.

    The easiest way to get the full path of the assembly and be certain of the result, is using System.Reflection.Assembly.GetEntryAssembly().Location.

    You can then use System.IO.Path.GetFileName and System.IO.Path.GetDirectoryName to get the parts of the path you need.

  • User profile image
    ScanIAm

    Thanks for the pointers.  C# has been a major boon to productivity after many years of c/c++, but the little quirks still get me.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.