Rob Mauii

I have not been able to find some documentation on how to pass parameters to a .Net windows forms executable. I am not sure what changes I need to make to the application to accept parameters

I want to be able to issue something like System.Diagnostics.Process.Start("c:\myfolder\myapp.exe 1 T") where in myapp.exe I am able to read the two parameters on startup and implement some logic accordingly.

Thanks in advance.



Re: Windows Forms General How-to Start .Net executable with parameters?

PJ. van de Sande


Process.Start(@"c:\myfolder\myapp.exe", "1 T");

     // - or -

Process p = new Process();
p.StartInfo.FileName = @"c:\myfolder\myapp.exe";
p.StartInfo.Arguments = "1 T";
p.Start();

 


Take a look at the MSDN page for more details.






Re: Windows Forms General How-to Start .Net executable with parameters?

Rob Mauii

Thank You for the quick reply. What are the changes I need to make in myapp.exe to accept and read in the parameters passed



Re: Windows Forms General How-to Start .Net executable with parameters?

PJ. van de Sande

You can always get the command line with Environment.CommandLine. But you can get them i you Main method as well:



using System;

class Sample
{
public static void Main( string[] args )
{
foreach( string arg in args )
{
Console.WriteLine( "Argument: " + arg );
}
}
}



When get it out of the args parameter in you main method, the parameters are nicelly splitted. Environment.CommandLine gives you the whole commandline and you need to split it yourself.





Re: Windows Forms General How-to Start .Net executable with parameters?

Rob Mauii

Not sure what I'm missing here but it does not seem to work I added in my project MyApp within the MainForm.vb (startup form for the project) some code as below (in VB.Net);

Public Sub MainForm(ByVal args() As String)

If args.Length = 0 Then

MessageBox.Show("Nothing", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)

Else

MessageBox.Show(args(0), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)

End If

From the other .Net application (Starter.exe) I issue the command; System.Diagnostics.Process.Start("C:\MyFolder\MyApp.exe", "1 T"); nothing displays (no messages at all)





Re: Windows Forms General How-to Start .Net executable with parameters?

Rob Mauii

Ok. it actually has to be called Main. SO if I change the above to

Public Shared Sub Main(ByVal args() As String)

Then everything works fine. Thanks for your help.





Re: Windows Forms General How-to Start .Net executable with parameters?

PJ. van de Sande

You figured it out. It must be an string array.

- Happy coding!





Re: Windows Forms General How-to Start .Net executable with parameters?

aajrb

That works fine for c# and it works fine for a console app but I cannot make it work for a windows program written with Visual basic.



Re: Windows Forms General How-to Start .Net executable with parameters?

aajrb

My.Application.CommandLineArgs