C# expanding skills: Command Line Options

thecoolnessrune

Diamond Member
Jun 8, 2005
9,652
553
126
Hey guys, I have rather rudimentary programming skills (fun hobby to me), and one of the things I was trying to do was have command line options in my program. Like how you can type bootsect.exe /NT60 H: to tell bootsect what to do.

I want to do something like that for a command line program I'm writing for ultrabasic cryptography. Basically all it does is take the program and then the command line arguments as a text file to open and then a number setting the cryptography key (anywhere from 1 to 100 or something like that).

Can anyone give a good description of how to get command line arguments in C#? I've simply never used them before and I can't seem to find any online resources that explain it in a way I can understand.

Thanks for shedding any light on this :)
 

thecoolnessrune

Diamond Member
Jun 8, 2005
9,652
553
126
So THATS why string[] args is always in the Main method of every program you start in VS 2005. My professor always told me "you wont be using this don't worry about it." Now I feel *stupid* that I didn't understand this before! /facepalm

Thanks Cogman. I appreciate that.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
13
81
www.markbetz.net
So THATS why string[] args is always in the Main method of every program you start in VS 2005. My professor always told me "you wont be using this don't worry about it." Now I feel *stupid* that I didn't understand this before! /facepalm

Thanks Cogman. I appreciate that.

Hahaha, you have to love some profs. It wouldn't have taken significantly more time to say "This is how arguments get passed in when running from the command line, but we'll skip over that for now."
 

Cogman

Lifer
Sep 19, 2000
10,277
125
106
Hahaha, you have to love some profs. It wouldn't have taken significantly more time to say "This is how arguments get passed in when running from the command line, but we'll skip over that for now."

:) Yep, I'm half convinced they don't really know what things like that does when it isn't in their lesson plans.
 

Kirby

Lifer
Apr 10, 2006
12,032
2
0
hmm, i figured there'd some wiz-bang .net way. not much different than argv/argc.
 

Cogman

Lifer
Sep 19, 2000
10,277
125
106
hmm, i figured there'd some wiz-bang .net way. not much different than argv/argc.

:) If it ain't broke, don't fix it.

If you REALLY want some wizbang way to get it, then there is the System.Environment.GetCommandLineArgs() method. However, since it is all setup in the application initialization, I don't really see the point of calling a separate function to get information that is available at the start of the program. It is almost like going the wrong way around to get constant values.
 

Leros

Lifer
Jul 11, 2004
21,867
7
81
:) If it ain't broke, don't fix it.

If you REALLY want some wizbang way to get it, then there is the System.Environment.GetCommandLineArgs() method. However, since it is all setup in the application initialization, I don't really see the point of calling a separate function to get information that is available at the start of the program. It is almost like going the wrong way around to get constant values.

I've used that function a few times. It has its places. :)
 

ASK THE COMMUNITY