Jun 18, 2010 02:48 AM
(This post was last modified: Jun 18, 2010 02:50 AM by ubbelol.)
Hey, i've had a few requests on making a tutorial on how to pass arguments on a VB.NET/C# console application so here we go:
It's actually ALOT more simple than it might sound, all you have to do is creating an string array that splits the arguments inputted.
Alright so here's the full code:
Now lets break it down into lines
The first line:
What that does is basically split the input that users type in by the char "-". You can change the char to whatever you want your args to be splitted with, example if you use "/" as a split char you enter args like:
The second line:
This simply tells the program to only execute the following code IF the strArray length (amouns of args inputted) is not less 1.
On to third:
Here we create a loop wich goes from 0 to the length of strArrays length (amouns of args inputted) and for each argument we execute the following code:
This outputs the current argument in strArray, strArray(0) would be "arg1" and strArray(1) would be "arg2" etc, i think you get the point.
The Trim() command removes any spaces in the start and end of the current argument so you won't get arguments like " arg1 " wich can be hard to use later on in the program.
And the last
Simply waits for the user to input anything, this is just to pause the window and not making it close instantly.
Heres a little video example of the console in action:
[IMG]http://i350.photobucket.com/albums/q408/ubbe12/tut1-13.gif[/IMG]
WTB Feedback. Sorry for my lame english..
It's actually ALOT more simple than it might sound, all you have to do is creating an string array that splits the arguments inputted.
Alright so here's the full code:
Code
Sub Main()
Dim strArray() As String = Command.Split("-")
If Not strArray.Length < 1 Then
For i = 0 To strArray.Length - 1
Console.WriteLine(strArray(i).Trim())
Next
End If
Console.ReadLine()
End SubNow lets break it down into lines
The first line:
Code
Dim strArray() As String = Command.Split("-")What that does is basically split the input that users type in by the char "-". You can change the char to whatever you want your args to be splitted with, example if you use "/" as a split char you enter args like:
Code
C:\path.exe /arg1 /arg2 /arg3The second line:
Code
If Not strArray.Length < 1 ThenThis simply tells the program to only execute the following code IF the strArray length (amouns of args inputted) is not less 1.
On to third:
Code
For i = 0 To strArray.Length - 1Here we create a loop wich goes from 0 to the length of strArrays length (amouns of args inputted) and for each argument we execute the following code:
Code
Console.WriteLine(strArray(i).Trim())This outputs the current argument in strArray, strArray(0) would be "arg1" and strArray(1) would be "arg2" etc, i think you get the point.
The Trim() command removes any spaces in the start and end of the current argument so you won't get arguments like " arg1 " wich can be hard to use later on in the program.
And the last
Code
Console.ReadLineSimply waits for the user to input anything, this is just to pause the window and not making it close instantly.
Heres a little video example of the console in action:
[IMG]http://i350.photobucket.com/albums/q408/ubbe12/tut1-13.gif[/IMG]
WTB Feedback. Sorry for my lame english..
Probably coding some useless shit.

