Click here to Skip to main content
15,895,871 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
VB
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:
VB
Private Sub ChkDBUtility() Handles cmdChkDBUtility.Click

    Dim iProcess As Process = Nothing
    If IsProcessRunning(mDBUtilName) = False Then
        ' the process is not running
        ' determine if the application is installed
        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
VB
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:
VB
Public Sub Main(ByVal args() As String)

And
VB
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.
Posted
Updated 20-Feb-18 22:06pm
v2
Comments
Maciej Los 20-Feb-18 16:04pm    
Why Process.Start(startInfo) gets only one argument? It should gets at least two arguments: one - application to run, second - argument.
Kevin Brady 20-Feb-18 17:14pm    
The application name is set in the initialization statement:
Dim startInfo As ProcessStartInfo = New ProcessStartInfo(mFullUtilPgmName)
Kevin Brady 20-Feb-18 17:17pm    
I got this from: https://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.arguments(v=vs.110).aspx
Maciej Los 21-Feb-18 3:16am    
What framework: WinForm, ConsoleApp, WPF, WebUI?
Kevin Brady 21-Feb-18 11:29am    
It is a console application. The start-up object is the Main sub. Is is net frame work 4.0 client profile.

1 solution

Check the sample code in the following :
ProcessStartInfo.Arguments Property (System.Diagnostics)[^]
 
Share this answer
 
Comments
Maciej Los 21-Feb-18 5:04am    
Richard, OP is using Arguments property in his code. Seems that problem is somewhere else.
Richard MacCutchan 21-Feb-18 5:49am    
Yes, that is obvious, but we need more details.
Maciej Los 21-Feb-18 5:56am    
It's obvious too ;)
Kevin Brady 21-Feb-18 11:15am    
Richard: The link you provided is the same as the link I mentioned. The code that I wrote in this post was derived from the link page.

What more details do either of you need? Where you able to run the code I posted successfully? Do you have a direction that you would suggest?
Richard MacCutchan 21-Feb-18 12:08pm    
Well I have just tried that code and it works perfectly, so there must be something else going on on your system.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900