Howto: Getting specific Command Line Argument using vb.net in a console application
Lately I’ve been struggling to find a built-in function in vb.net that, in a console application, would return a specific command line argument. However – I did’nt find it.
Instead, I wrote this piece of code to do the function, that can be used when calling the .exe like this: "c:\programname.exe /file:c:\folder\filename.txt"
”’ <summary>
”’ A function that returns a specific Command Line Argument from format c:\commandname.exe /arg:value
”’ </summary>
”’ <param name="arg">The name of the argument to return</param>
”’ <param name="oArgs">An arraylist of arguments</param>
”’ <returns>String</returns>
”’ <remarks></remarks>
Private Function ReturnSpecificCommandLineArg(ByVal arg As String, ByVal oArgs As ArrayList) As StringDim _ReturnString As String = String.Empty
Try
With oArgs‘We don’t really need to sort – but hey!
.Sort()‘Looping throug the array to find the arguments
For i As Integer = 0 To .Count – 1If InStr(.Item(i).ToString, arg) > 0 Then
‘Getting the specific value
_ReturnString = .Item(i).ToString.Substring(InStr(.Item(i).ToString, ":"))‘We’ve found the value – so we need to exit our for-loop
Exit ForElse
‘Returning an empty string if we did’nt find what we were looking for
_ReturnString = String.Empty
End IfNext
End WithReturn _ReturnString
Catch ex As Exception
‘Handling errors
Throw New Exception(ex.Message, ex.InnerException)End Try
End Function