How to Parse Command Line Parameters?

sma_soft

New member
Joined
Sep 8, 2005
Messages
1
Location
Iran
Programming Experience
5-10
How to Parse Command Line Parameters in VB.NET ?

Hi dudes,
I want to know how to parse command lines parameters passed to a Console Application in Visual Basic .NET ?
in C# it is possible to parse them with the "string[] args" argument in the "static void main()" function.
Which the Args variable is an array of strings, containing the passed command line arguments sequentially.


This is the C# Console Application Code:

[STAThread]

static void Main(string[] args)

{

//

}

But do you know what is the Equivalent of Args variable in a VB.NET console application ?
thanks for help anyone.


This is the VB.NET Code:

Module Module1



Sub Main()



End Sub



End Module
 
Hi ,

Here it is
VB.NET:
[size=2][color=#0000ff]Module[/color][/size][size=2] Module1

  [/size][size=2][color=#0000ff]Sub[/color][/size][size=2] Main()

    [/size][size=2][color=#0000ff]Dim[/color][/size][size=2] s() [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]String[/color][/size][size=2] = System.Environment.GetCommandLineArgs()

    Console.WriteLine(s(1))

  [/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub

End[/color][/size][size=2] [/size][size=2][color=#0000ff]Module

[/color][/size]

Thanks
Jay
 
VB.NET:
Dim strStartupArguments() As String, intCount As Integer
strStartupArguments = System.Environment.GetCommandLineArgs
		For intCount = 0 To UBound(strStartupArguments)
			Select Case strStartupArguments(intCount).ToLower
				Case "-arg1"
				    'Do your code for the arg1 argument
			End Select
		Next intCount

the first one is the application path&filename
 
In VB.NET 2005 (.NET Framework 2.0), also consider using My.Application.CommandLineArgs

For example:
VB.NET:
Imports System.Console
 
Module Module1
 
Sub Main()
Dim argc As Integer
Dim argv As System.Collections.ObjectModel.ReadOnlyCollection(Of String)
 
argv = My.Application.CommandLineArgs
argc = argv.Count
 
If argc > 0 AndAlso argv(0) = "/?" Then
Write("Usage: ")
Write(My.Application.Info.AssemblyName)
Write("[ /log filename]")
End If
End Sub
 
Last edited by a moderator:
I experience 'My.Application.CommandLineArgs' to be seriously flawed, it get confused as example when parameters and quotes are mixed together.. for example:
MyExecutable.exe -xml="C:\foo\foobar.xml" -load
This will be wrongly splitted into two arguments, -xml= AND the rest of the string --> C:\foo\foobar.xml" -load

I use this drop-down replacement of My.Application.CommandLineArgs having a much more nice behaviour using complex command lines.

'Better replacement of --> My.Application.CommandLineArgs
Public Function CommandLineArgs() As System.Collections.ObjectModel.ReadOnlyCollection(Of String)
Dim value As String = Environment.CommandLine
Dim bInsideQuote As Boolean
Dim iCnt As Integer
If value = "" Then Return New System.Collections.ObjectModel.ReadOnlyCollection(Of String)(Nothing)
For Each c As Char In value.ToCharArray
iCnt += 1
If c = """"c Then
bInsideQuote = Not bInsideQuote
Continue For
End If
If c = " " Then
If bInsideQuote Then
Continue For
Else
Mid$(value, iCnt, 1) = "§"
End If
End If
Next
'Replace multiple separators
While value.IndexOf("§§") > 0
value = value.Replace("§§", "§")
End While
Dim CmdArgs As New List(Of String)
Dim FirstArg As Boolean = True
For Each sArg As String In value.Split("§".ToCharArray)
If FirstArg Then
FirstArg = False
Continue For
End If
CmdArgs.Add(RemoveQuote(sArg))
Next
Return New System.Collections.ObjectModel.ReadOnlyCollection(Of String)(CmdArgs)
End Function

Then, you can cycle on string collection and easily parse arguments one by one.

Hope this helps,

Paolo Marani
 
VERY old thread, but none has mentioned arguments can be included straight from the Main method: Main Procedure in Visual Basic
Main can also take a String array as an argument.

Function Main(ByVal cmdArgs() As String) As Integer

or

Sub Main(ByVal cmdArgs() As String)
This has been valid Main methods in all VB versions by the way.
 
I experience 'My.Application.CommandLineArgs' to be seriously flawed, it get confused as example when parameters and quotes are mixed together.. for example:
MyExecutable.exe -xml="C:\foo\foobar.xml" -load
This will be wrongly splitted into two arguments, -xml= AND the rest of the string --> C:\foo\foobar.xml" -load
Both My.Application.CommandLineArgs and using the Main method args parameter produce same result, ie these two (correct) arguments:
-xml=C:\foo\foobar.xml
-load
 
Back
Top