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

How to Add Command Line Aurguments to your VB.Net Code in Visual Basic Language

I have looked on a few VB forums and Googled till my eyes started bleeding. There is almost no information or examples of how to add command line arguments to your project so you can run functions of a compiled .exe file. Here is some code I have been working on to add the arguments. Very simple functions are to hide the form and click button1 on load.

Private Sub frmForm1_Load(sender As Object, e As EventArgs) Handles Me.Load
        CommandLineArgs()
    End Sub

Public Function CommandLineArgs()
        Dim clArgs As String = Environment.CommandLine
        For Each s As String In clArgs
            Select Case s.ToLower
                Case "/r"
                    Button1.PerformClick()
                Case "/h"
                    Me.Visible = False
            End Select
        Next
        Return CommandLineArgs(clArgs)
    End Function

Open in new window


Here is a screenshot of the Project Properties window with the Command Line arguments. This does not help it function in debug mode.

Project Properties Window>Debug>Start Options>Command Line Arguments
Any help on this would be greatly appreciated. (My code doesn't work) If anyone knows the correct method for creating these switches please help me.
0
Steve Williams
Asked:
Steve Williams
2 Solutions
 
Éric MoreauSenior .Net ConsultantCommented:
0
 
Fernando SotoCommented:
Hi Steve;

Try the method like this. The command line args are available any time you need it via the Environment.CommandLine as a string or Environment.GetCommandLineArgs as an array. This line of code, Return CommandLineArgs(clArgs), is calling the method Recursively, I do not think this is what you wanted to do.
Public Sub CommandLineArgs()
    Dim clArgs As String() = Environment.GetCommandLineArgs
    For Each s As String In clArgs
        Select Case s.ToLower
            Case "/r"
                Button1.PerformClick()
            Case "/h"
                Me.Visible = False
            Case Else
                Console.WriteLine(s)
        End Select
    Next
End Sub

Open in new window

0
 
Steve WilliamsProduct Design EngineerAuthor Commented:
Eric, your solution was way more than I needed. I'm sure for a huge application it would be justified but my application is just a simple tool that has less than 75 lines of code.

Fernando Soto's solution was quick and simple and it worked like a charm.

 I give Eric kudos for all the work he did on that article it was very nice and it has downloadable code which I'm going to put in my library for future use when and if I ever get that in depth in coding. I'm a Mechanical engineer by trade and only dabble in coding to support my Engineering requirements. I will say tho, coding is a lot of fun and I really enjoy the time I get to spend with it.

As always, you guys rock thanks again for your help.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

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