Executing a program with command line options

nem33

Newcomer
Joined
Mar 17, 2003
Messages
23
For instance:

"C:\MyProgram.exe" option1 option2 option3

I'm trying to do this within a form

I've tried this method but can't pass any arguments, can only execute the program without options:

Code:
filename = "C:\MyProgram.exe"
System.Diagnostics.Process.Start(filename)

Now If I do this:
Code:
filename = "C:\MyProgram.exe option1 option2"
System.Diagnostics.Process.Start(filename)

It cannot open the program at all.
 
Process.Start is overloaded to also take two string parameters.
When this overload is used, the second parameter of the
function is arguments passed to the program being started.

Visual Basic:
Dim filename As String = "C:\MyProgram.exe"
Dim args As String = "option1 option2"
System.Diagnostics.Process.Start(filename, args)
 
Here is how I handle it in my program.

Module Startup

Public Sub Main()
Dim Result As DialogResult
Dim Arguments As Object
Dim i As Integer
Arguments = Environment.GetCommandLineArgs()

Dim f As New Form1()


If IsArray(Arguments) Then
For i = LBound(Arguments) To UBound(Arguments)
Select Case i
Case 1
curProject = Arguments(i)
If curProject.Length <> 6 Then
MessageBox.Show("No current project is selected, " & vbCrLf & _
"Please select a project", "Missing project information!", MessageBoxButtons.OK, MessageBoxIcon.Error)
End
End If
Case 2
ProductType = Arguments(i)
'Need to get the first character of the passed argument
ProductType = ProductType.Substring(0, 1)

If ProductType <> "C" Then

ElseIf ProductType <> "T" Then

ElseIf ProductType <> "W" Then

ElseIf ProductType <> "X" Then
MessageBox.Show("Invalid product type, " & vbCrLf & _
"Please select a project", "Missing project information!", MessageBoxButtons.OK, MessageBoxIcon.Error)
End
End If
Case 3
Phase = Arguments(i)
If Phase.Length <> 2 Then
MessageBox.Show("Invalid Phase id, " & vbCrLf & _
"Please select a project", "Missing project information!", MessageBoxButtons.OK, MessageBoxIcon.Error)
End
End If
End Select
Next

End If

f.ShowDialog()

End Sub

End Module
 
The above code has some errors in it:
Visual Basic:
Public Class Startup 'Use classes not modules, modules are wrapped around a class anyway

Public Shared Sub Main()
Dim Result As DialogResult
Dim Arguments As String() 'Don't use Objects, Objects will cause boxing and unboxing which slows things down
Dim i As Integer
Arguments = Environment.GetCommandLineArgs()

Dim f As Form1 = New Form1()

If Arguments.GetUpperBound(0) > 0 Then 'I'm pretty sure IsArray is a compatibility function
For i = Arguments.GetLowerBound(0) To Arguments.GetUpperBound(0) 'LBound and UBound are compatibility functions
Select Case i
Case 1
curProject = Arguments(i)
If curProject.Length <> 6 Then
MessageBox.Show("No current project is selected, " & vbCrLf & _
"Please select a project", "Missing project information!", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return 'End is a compatibility command, it shouldn't be used
End If
Case 2
ProductType = Arguments(i)
'Need to get the first character of the passed argument
ProductType = ProductType.Substring(0, 1)

If ProductType <> "C" Then

ElseIf ProductType <> "T" Then

ElseIf ProductType <> "W" Then

ElseIf ProductType <> "X" Then
MessageBox.Show("Invalid product type, " & vbCrLf & _
"Please select a project", "Missing project information!", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End If
Case 3
Phase = Arguments(i)
If Phase.Length <> 2 Then
MessageBox.Show("Invalid Phase id, " & vbCrLf & _
"Please select a project", "Missing project information!", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End If
End Select
Next

End If

'Don't use ShowDialog unless it's a secondary form
Application.Run(f)

End Sub

End Class
He was asking how to send commandline parameters, not how to read them though
 
Back
Top