Starting an exe with args

NachitoMax 411 Reputation points
2022-08-13T23:01:24.593+00:00

Hey

Im calling my App from another project with an arg. The App being opened is checking the arg to valid it can be opened. Something like this-

The code in app1 calling App2 with args

Dim App As New Process()  
App.StartInfo.FileName = ""C:\PathToMyApp.exe"  
App.StartInfo.Arguments = "Argument1"  
App.Start()  

In the App that is being opened with the args, the code is this-

Private Sub MyApp_Startup(ByVal sender As Object, ByVal e As StartupEventArgs) Handles Me.Startup  
  
Dim val As String = String.Empty  
For Each arg As String In e.CommandLine  
   val = CType(arg.ToString, String)  
Next  
  
'Do something to validate the incoming arg  
End Sub  

My question is, if i want to pass multiple args, do i pass a single string from App1 in the 'App.StartInfo.Arguments'? Something like-
"Argument1, Argument2, Argument3"

My other question is, When im splitting the arguments in the receiving App2, Is the args an array that i can split or is a simple case of splitting the string at the comma?

Thanks

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,415 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,126 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. NachitoMax 411 Reputation points
    2022-08-14T04:43:04.51+00:00

    Hey

    I found my answer-

    Send the args as a single string separated with a comma. this can also be a constructed string as long as the Argument is a string

     Dim App As New Process()  
     App.StartInfo.FileName = "C:\PathToMyApp.exe"  
     App.StartInfo.Arguments = "Argument1, Argument2, False, etc"  
     App.Start()  
    

    Get each arg from the array in your receiving App
    Ignore Arg(0) as this is usually the path of the App being opened

     Private Sub MyApp_Startup(ByVal sender As Object, ByVal e As StartupEventArgs) Handles Me.Startup  
              
        Dim args As String() = System.Environment.GetCommandLineArgs  
          
        Dim GetArg1 As String = args(1)  
        Dim GetArg2 As String = Args(2)  
        Dim GetArg3 As Boolean= CType(Args(3), Boolean)  
        Dim GetArg4 As String = Args(4)  
      
     End Sub  
    
    0 comments No comments