Command Line Args in Windows Forms?

techwiz24

Well-known member
Joined
Jun 2, 2010
Messages
51
Programming Experience
Beginner
I was playing a game recently and saw that they could use args in the shortcut, and I decided to try to impliment this into my application...so far, I got: (runs this sub on load)
VB.NET:
Sub startup_args()
        If Environment.GetCommandLineArgs.Contains("+game") Then
            Dim path As Array = Environment.GetCommandLineArgs
            MsgBox(path)
        End If
    End Sub

just to see what it returns, but the application crashes. I also tried path.tostring(), but that didn't work. When I did:
VB.NET:
 If Evnironment.GetCommandLineArgs.Contains("+game") Then
   Dim path as string = Environment.GetCommandLineArgs.tostring
   MsgBox(path)
End If
End Sub

but that returned the value "SString.Array[]"...what am I doing wrong?
 
Environment.GetCommandLineArgs is a string array, not a plain string. You need to loop the array:
VB.NET:
For Each Str As String In Environment.GetCommandLineArgs()
    Select Case Str.ToLower
        Case "+game"
            'Code
    End Select
Next Str
 
Just change the loop to use the index instead:
VB.NET:
For Counter As Integer = 0I To Environment.GetCommandLineArgs().Length - 1I
    Select Case Environment.GetCommandLineArgs(Counter).ToLower
        Case "+game"
            'Use Counter + 1
            Counter += 1I 'Skip the next element since we already grabbed the game
    End Select
Next Counter
 
soo...if the args contains "+game" then...it skips the rest? then, what would I use to return the rest as a string? Sorry :D I self taught myself, and am learning new things every day XD
No, if it finds it then you can grab the next element (the path to the game) then it increments the counter (to skip the element that has the game path) and continues on in the array.
 
Ok, I finally got it working:
VB.NET:
Sub startup_args()
        Dim events As Array = Environment.GetCommandLineArgs
        Try
            If events(1).ToString = "+game" Then
                GameViewer.Movie = events(2).ToString
                GameViewer.Play()
                uArgs = "yes"
            End If
        Catch ex As Exception

        End Try
    End Sub

the var uArgs is a public string to tell it to play the defult game or not.

I did some research and found that arrays are like multipule strings in one, so:
VB.NET:
Dim test as array
test(1) = hi
test(2) = test
and so on. Thanks JuggaloBrotha for pointing me in the right direction!
 
Back
Top