I am testing a simple vb.net console application (call it the "child" ). I want to pass it some arguments from another application (i.e. the "Parent") and have it return an exit code.
Here is the code for the child app:
Public Sub Main()
Environment.ExitCode = 0
Dim tstr As String = ""
For i As Short = 0 To My.Application.CommandLineArgs.Count - 1 Step 1
tstr &= My.Application.CommandLineArgs(i) & vbCrLf
Next
If My.Application.CommandLineArgs.Count > 0 Then
MsgBox("Number of Arguments: " & My.Application.CommandLineArgs.Count & vbCrLf & "Arguments: " & tstr)
Environment.ExitCode = 1
End If
End
End Sub
Here is the code for the calling method in the parent app:
Private Sub ChkDBUtility() Handles cmdChkDBUtility.Click
Dim iProcess As Process = Nothing
If IsProcessRunning(mDBUtilName) = False Then
If IsFile(mFullUtilPgmName) = True Then
Dim startInfo As ProcessStartInfo = New ProcessStartInfo(mFullUtilPgmName)
startInfo.WindowStyle = ProcessWindowStyle.Normal
startInfo.Arguments = "Test123"
iProcess = Process.Start(startInfo)
iProcess.WaitForExit()
If iProcess.HasExited Then
Dim i As Integer = iProcess.ExitCode
MsgBox("Return code = " & i)
End If
End If
End If
End Sub
Note: the variables "mDBUtilName" and "mFullUtilPgmName" are assigned elsewhere in the parent app and are correct.
The child application does not seem to get the passed parameters. That is, the
CommandLineArgs.Count
is never greater than zero.
What I have tried:
I have tried different ways of trying to get the command line arguments in the child app including:
Public Sub Main(ByVal args() As String)
And
Dim arguments As String() = Environment.GetCommandLineArgs()
in the Main sub.
I have also run the child app in debug mode in visual studio and set the command line arguments on the debug tab of the project. That seems to work okay.
I have tried wrapping the passed parameter in triple quotes.