Thursday, 23 April 2009

Windows forms application using command line arguments

This is one of those (many) little things I do infrequently enough to have to 're-learn' it every time as I just can't quite remember the syntax. But every time I google it I seem to find lots of console application examples like this:

Public Sub Main(ByVal args() As String)
'do stuff
End Sub

Well, for the (or rather my) record, here's how you do it in a winforms app:

Dim args() As String = Environment.GetCommandLineArgs()

3 comments:

  1. You can still do it the first way, just change your Main method signature from static void Main() to static void Main(string[] args) (this is in C#, similar for VB I assume)

    Then you can pass the arguments or whatever specific arguments you need onto your form.

    But your second way seems like a handy shortcut, thanks.

    ReplyDelete
  2. Thanks Rod, more specifically I guess I'm referring to windows forms apps where the startup object is a form rather a Sub Main procedure.

    AFAIK Environment.GetCommandLineArgs() can be called at any point or form throughout your process and will return the commandline arguments that were supplied at startup, which makes it doubly handy :)

    ReplyDelete
  3. STARREd and FAVORITEd, thanks for the note.

    I just dealt with this tidbit as well!
    Jose - cadkicks.com

    ReplyDelete

Comments are moderated, so you'll have to wait a little bit before they appear!