• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 335
  • Last Modified:

how to read console input

i'm trying to make a console app that basically works like this:

user types app name in console, example PrintDoc
following PrintDoc is a url and number, so it might look something like this

PrintDoc http://www.google.com 3

that basically tells the program to print 3 copies of http://www.google.com

i'm not sure how to pull the http://www.google.com and the 3 when the user inputs it, any suggestions?
i'm very new to vb programming, this is a .net app

thanks
0
LTY83
Asked:
LTY83
  • 2
1 Solution
 
Erick37Commented:
Use the Command() function to read in the command line parameters.
Simple demo using the function GetCommandLineArgs() from MS help.

Module Module1

    Sub Main()
        'Read command line args (if any)
        Dim args() As String = GetCommandLineArgs()
        'Test to see if any args passed in
        If IsArray(args) Then
            Dim i As Integer
            For i = args.GetLowerBound(0) To args.GetUpperBound(0)
                'print out each command line arg
                Console.WriteLine(args(i))
            Next
        End If
    End Sub

    'This function taken from MS on-line help files
    Function GetCommandLineArgs() As String()
        ' Declare variables.
        Dim separators As String = " "
        Dim commands As String = Microsoft.VisualBasic.Command()
        Dim args() As String = commands.Split(separators.ToCharArray)
        Return args
    End Function


End Module
0
 
Erick37Commented:
Much simpler... I forgot you could just pass in the array directly int Main()

Module Module1

    Sub Main(ByVal args() As String)
        'Dim args() As String = GetCommandLineArgs()
        If IsArray(args) Then
            Dim i As Integer
            For i = LBound(args) To UBound(args)
                'print out each command line arg
                Console.WriteLine(args(i))
            Next
        End If
    End Sub


End Module
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now