Results 1 to 4 of 4

Thread: [RESOLVED] [2.0] Passing arguements to an application

  1. #1

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Resolved [RESOLVED] [2.0] Passing arguements to an application

    Given the following code:

    VB Code:
    1. [STAThread]
    2. public static void Main(string[] args)
    3. {
    4.     MessageBox.Show("Arguements via args[] = " + args.Length + "\nArguements via Environment.GetCommandLineArgs() = " + Environment.GetCommandLineArgs().Length.ToString());
    5. }

    Now I'm a bit confused. I have a large application written with C# and .Net 1.1 (BinaryEdit and I use the string[] args to launch different options. I also don't launch anything unless there is at least 1 arguement (which should be the path of the executable). This works great.

    In a new application I'm developing, with C# and .Net 2.0, this code returns 0 for args and 1 for the GetCommandLineArgs(). Why?

    An even odder question... I tried to re-create this with 1.1 and C# and if I run this exact code in a new project with 1.1, it returns the same as the 2.0 (0 for the args and 1 for the GetCommandLineArgs()).

    So it would appear that this works great for my large application with C# 1.1 but it does not work with any new C# 1.1 projects (windows or console) or my new C# 2.0 project.

    Should I be using Environment.GetCommandLineArgs() over passing args to the Main() method? Any insight into my issue?
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Passing arguements to an application

    The argument list passed to the Main method does not include the executable path. The argument list returned by GetCommandLine Args does. I've tested that on 2003 and 2005 to confirm. I can't see how any previous applications of your would behave any differently. I guess the ".NET way" would be to use GetCommandLineArgs and that is what I'd do, but most coming from a C/C++/Java background would probably be inclined to use the Main arguments. I can't think of many situations where you would need to know the executable path as a commandline argument so from that point of view GetCommandLineArgs is probably a little less efficient.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: [2.0] Passing arguements to an application

    Yeah I just finished reviewing my code and found I do account for this so it doesn't work in my other app, lol.

    Thanks
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  4. #4
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: [RESOLVED] [2.0] Passing arguements to an application

    There's no reason not to use the main(...) args to get the commandline params. Its perfectly acceptable .NET practice.

    Efficiency is a bit irrelevant here really because you're only going to pull the args once at startup time anyway, they don't change over time.
    I don't live here any more.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width