Command Line Arguments


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: Command Line Arguments

  1. #1
    Kevin Guest

    Command Line Arguments


    Hi I want the user to be able to enter in command line arguments. I have
    a program and I want to be able to have the user decide if he/she wants to
    use command line arguments. If he/she chooses to use command line arguments
    then I want to hide the form(oh yeah, how do I do that too?). I want to
    be able to do this in VB.NET, how do I do this? Thanks
    Kevin

  2. #2
    Patrick Steele [MVP] Guest

    Re: Command Line Arguments

    In article <3d3d708b$1@10.1.10.29> (from Kevin <Kevin@kevin.com>),
    >
    > Hi I want the user to be able to enter in command line arguments. I have
    > a program and I want to be able to have the user decide if he/she wants to
    > use command line arguments. If he/she chooses to use command line arguments
    > then I want to hide the form(oh yeah, how do I do that too?). I want to
    > be able to do this in VB.NET, how do I do this? Thanks


    The command line arguments can be easily obtained using
    System.Environment.GetCommandLineArgs. This returns you a string array
    of all of the command line arguments.

    As for displaying a UI, that depends on how your app starts up. If
    you've written your own Main(), it probably looks something like this:

    Public Shared Sub Main()
    Application.Run(New Form1())
    End Sub

    If you simply set "Form1" as your startup project, VB.NET actually
    creates a Sub Main() for you with the code shown above. If you want to
    do something based on command-line arguments before (or instead of)
    showing the UI, create a Main() method as shown above, but do some logic
    before starting the form:

    Public Shared Sub Main()
    ' do work here
    ' if work completed, exit sub

    ' otherwise...
    Application.Run(New Form1())
    End Sub

    --
    Patrick Steele
    Microsoft .NET MVP
    http://radio.weblogs.com/0110109

  3. #3
    Kevin Guest

    Re: Command Line Arguments


    Hi I am still quite confused. I was able to create a command prompt using
    the Shell code ---> Shell ("cmd"). However, I still cannot hide the form
    when the command prompt comes up, and I cannot get the command prompt to
    start at "c:\", but rather it starts at the location of my visual studio
    project...How do I fix this? Thanks
    Kevin


    Patrick Steele [MVP] <patrick@mvps.org> wrote:
    >In article <3d3d708b$1@10.1.10.29> (from Kevin <Kevin@kevin.com>),
    >>
    >> Hi I want the user to be able to enter in command line arguments. I have
    >> a program and I want to be able to have the user decide if he/she wants

    to
    >> use command line arguments. If he/she chooses to use command line arguments
    >> then I want to hide the form(oh yeah, how do I do that too?). I want

    to
    >> be able to do this in VB.NET, how do I do this? Thanks

    >
    >The command line arguments can be easily obtained using
    >System.Environment.GetCommandLineArgs. This returns you a string array


    >of all of the command line arguments.
    >
    >As for displaying a UI, that depends on how your app starts up. If
    >you've written your own Main(), it probably looks something like this:
    >
    >Public Shared Sub Main()
    > Application.Run(New Form1())
    >End Sub
    >
    >If you simply set "Form1" as your startup project, VB.NET actually
    >creates a Sub Main() for you with the code shown above. If you want to


    >do something based on command-line arguments before (or instead of)
    >showing the UI, create a Main() method as shown above, but do some logic


    >before starting the form:
    >
    >Public Shared Sub Main()
    > ' do work here
    > ' if work completed, exit sub
    >
    > ' otherwise...
    > Application.Run(New Form1())
    >End Sub
    >
    >--
    >Patrick Steele
    >Microsoft .NET MVP
    >http://radio.weblogs.com/0110109



  4. #4
    Larry Serflaten Guest

    Re: Command Line Arguments

    "Kevin" <vvvvvvbbbbbbb@vb.com> wrote
    > Hi I am still quite confused. I was able to create a command prompt using
    > the Shell code ---> Shell ("cmd"). However, I still cannot hide the form
    > when the command prompt comes up, and I cannot get the command prompt to
    > start at "c:\", but rather it starts at the location of my visual studio
    > project...How do I fix this? Thanks


    Your confusion is contagious. I've read the entire thread and I still don't know
    what you are trying to do.

    None the less, try this for a button event:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles Button1.Click

    Shell(Environment.GetEnvironmentVariable("COMSPEC") & " /K chdir c:\", AppWinStyle.NormalFocus)
    Me.Close()

    End Sub


    LFS



  5. #5
    Tom Shelton Guest

    Re: Command Line Arguments


    "Kevin" <Kevin@kevin.com> wrote in message news:3d3d708b$1@10.1.10.29...
    >
    > Hi I want the user to be able to enter in command line arguments. I have
    > a program and I want to be able to have the user decide if he/she wants to
    > use command line arguments. If he/she chooses to use command line

    arguments
    > then I want to hide the form(oh yeah, how do I do that too?). I want to
    > be able to do this in VB.NET, how do I do this? Thanks
    > Kevin


    Public Class Form1
    Inherits System.Windows.Form

    .... Windows Generated code....

    ' Add something like this to your form
    Public Shared Sub Main(args() As String)

    If args.Length > 0 Then
    ' Replace following with whatever you want to do
    ' if they pass command line args
    Dim i As Integer
    For i = 0 To args.GetUpperBound(0)
    MessageBox.Show(arg(i))
    Next
    Else
    ' Show the form
    Application.Run(new Form1())
    End If

    End Sub

    End Class

    HTH,
    Tom Shelton



  6. #6
    Kevin Guest

    Re: Command Line Arguments


    Hi Tom, I want to hide the form if the user decides to choose the command
    prompt option...So far I have,
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Handles MyBase.Load
    strQuestion = "Do you want to search using command line arguments
    or using the program? Enter in Yes to use command line arguments and click
    OK or to use the program just click OK."
    strYes = UCase("Yes")
    If UCase(InputBox(strQuestion, strQuestion)) = strYes Then
    Shell("cmd")

    End If
    End Sub
    However, the Command Prompt starts in the file that my vb.net project is
    in and I want it to start in the basic "c:\" folder. What I think you wrote
    is to hide the form, but where would I put that in my coding? Thanks
    Kevin

    "Tom Shelton" <tom@mtogden.com> wrote:
    >
    >"Kevin" <Kevin@kevin.com> wrote in message news:3d3d708b$1@10.1.10.29...
    >>
    >> Hi I want the user to be able to enter in command line arguments. I have
    >> a program and I want to be able to have the user decide if he/she wants

    to
    >> use command line arguments. If he/she chooses to use command line

    >arguments
    >> then I want to hide the form(oh yeah, how do I do that too?). I want

    to
    >> be able to do this in VB.NET, how do I do this? Thanks
    >> Kevin

    >
    >Public Class Form1
    > Inherits System.Windows.Form
    >
    > .... Windows Generated code....
    >
    > ' Add something like this to your form
    > Public Shared Sub Main(args() As String)
    >
    > If args.Length > 0 Then
    > ' Replace following with whatever you want to do
    > ' if they pass command line args
    > Dim i As Integer
    > For i = 0 To args.GetUpperBound(0)
    > MessageBox.Show(arg(i))
    > Next
    > Else
    > ' Show the form
    > Application.Run(new Form1())
    > End If
    >
    > End Sub
    >
    >End Class
    >
    >HTH,
    >Tom Shelton
    >
    >



  7. #7
    Kevin Guest

    Re: Command Line Arguments


    Larry you solved the problem, I appreciate it! Now I am just trying to work
    on Message Boxes but I should be able to figure this out. Thanks for the
    help.
    Kevin

    "Larry Serflaten" <serflaten@usinternet.com> wrote:
    >"Kevin" <vvvvvvbbbbbbb@vb.com> wrote
    >> Hi I am still quite confused. I was able to create a command prompt using
    >> the Shell code ---> Shell ("cmd"). However, I still cannot hide the form
    >> when the command prompt comes up, and I cannot get the command prompt

    to
    >> start at "c:\", but rather it starts at the location of my visual studio
    >> project...How do I fix this? Thanks

    >
    >Your confusion is contagious. I've read the entire thread and I still don't

    know
    >what you are trying to do.
    >
    >None the less, try this for a button event:
    >
    > Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As

    System.EventArgs)
    >_
    > Handles Button1.Click
    >
    > Shell(Environment.GetEnvironmentVariable("COMSPEC") & " /K chdir

    c:\", AppWinStyle.NormalFocus)
    > Me.Close()
    >
    > End Sub
    >
    >
    >LFS
    >
    >



  8. #8
    Patrick Steele [MVP] Guest

    Re: Command Line Arguments

    In article <3d3dab58$1@10.1.10.29> (from Kevin <vvvvvvbbbbbbb@vb.com>),
    > Hi I am still quite confused. I was able to create a command prompt using
    > the Shell code ---> Shell ("cmd"). However, I still cannot hide the form
    > when the command prompt comes up, and I cannot get the command prompt to
    > start at "c:\", but rather it starts at the location of my visual studio
    > project...How do I fix this?


    If the user is going to use command-line parameters to execute your app,
    they're going to have to supply them *before* your app runs -- you can't
    "display" a command prompt and allow them to type in values. For
    example:

    C:\>yourapp /p1 /p2 /p3

    The application "yourapp" is started and the command-line args "/p1",
    "/p2" and "/p3" are available for you to do something with (see Tom
    Shelton's post).

    --
    Patrick Steele
    Microsoft .NET MVP
    http://radio.weblogs.com/0110109

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center