?
Solved

How to add command line arguments to a Windows Application with Visual Studio 2005, VB.NET

Posted on 2008-10-22
4
Medium Priority
?
598 Views
Last Modified: 2013-11-26
I want my application to have command line arguments so that it can be started at different points. Such as...
If the application name is myapplication.exe, then I want it to be able to have arguments to specify at which point the application should start at, like myapplication.exe -k. And if they enter -k, then it starts at a certain sub or class in the application. Or if they enter myapplication.exe -f, then it will start at a different sub or class in the application.
0
Comment
Question by:bryanpilger
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 19

Expert Comment

by:Stephen Manderson
ID: 22775876
You could do something like this..

Regards
Steve
Dim Arguments() As String = Environment.GetCommandLineArgs()
   For Each Argument As String In Arguments
      If Argument = "-f" Then
         'Your conditional
      End If
   Next

Open in new window

0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 22775882
0
 
LVL 19

Accepted Solution

by:
Stephen Manderson earned 2000 total points
ID: 22776072
Sorry forgot to include that you should put the check in the sub main and set that as the startup object.

Regards
Steve
Public Class Loader
 
    Public Shared Sub Main()
 
        Dim Arguments() As String = Environment.GetCommandLineArgs()
 
        ' If the argument lenght is 1 then only the file name is present
        If Arguments.Length = 1 Then
            ' Do your default load
            MsgBox("Load Default")
 
            ' If a command line argument has been provided and is a valid argument ie -k...
        ElseIf Arguments.Length = 2 AndAlso (Arguments(1) = "-k" Or Arguments(1) = "/k") Then
            ' Do your k load..
            MsgBox("Loading K Option")
 
        End If
 
    End Sub
 
End Class

Open in new window

0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 22777224
For VB.Net 2005 (and above), read up on the Application Model:
http://msdn.microsoft.com/en-us/library/w3xx6ewx(VS.80).aspx

You can get the command line arguments in the Startup() event, which runs BEFORE the startup object (Form) is shown.  From there, you can set a SHARED variable in your main form that you can use to make decisions with:
http://msdn.microsoft.com/en-us/library/t4zch4d2(VS.80).aspx
0

Featured Post

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question