|
-
Mar 27th, 2006, 05:39 PM
#1
[RESOLVED] [2.0] Passing arguements to an application
Given the following code:
VB Code:
[STAThread]
public static void Main(string[] args)
{
MessageBox.Show("Arguements via args[] = " + args.Length + "\nArguements via Environment.GetCommandLineArgs() = " + Environment.GetCommandLineArgs().Length.ToString());
}
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?
-
Mar 27th, 2006, 06:11 PM
#2
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.
-
Mar 27th, 2006, 06:26 PM
#3
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
-
Mar 28th, 2006, 06:49 AM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|