Goal: To pass parameters between WinForm programs. So that the child process can make the appropriate processing.
A wrong way.
Main program of Parent process:
1 New ProcessStartInfo (); 2 " ProcessChild.exe " ; 3 Psi. Arguments = Txtargs.text; 4 Process.Start (PSI); // The main problem here
Main program of child process:
1 txtargs.text = process.getcurrentprocess (). startinfo.arguments;
Results
The reason for the error is that it is assumed that the ProcessStartInfo of the parent process's instance of this class arguments passed into the child process. In fact Process.Start () returns an object of process type, the data is stored in the returned object, and is not passed across the process.
Two of the right ways
The first type:
The incoming data is received from main (string []args). The main method to modify the subprocess here is as follows:
1 Static void Main (string []args)2 {3 Application.enablevisualstyles (); 4 Application.setcompatibletextrenderingdefault (false); 5 Application.Run (new frmchild ()); 6 }
Because the default is no parameter. To save the string in args, it is worth mentioning that args always has at least one element, and the second method is easy to see.
The second type: Use the Environment class method.
1 string [] args = Environment.getcommandlineargs (); 2 // Txtargs.text = process.getcurrentprocess (). startinfo.arguments; 3 Txtargs.text = args[0"\ r \ n";
When not started from the parent process, the result is as follows: the number of elements in args is 1.
When started from the parent process, the parameter passed by the parent becomes the second element of args: The code in the child process
1 string[] args =Environment.getcommandlineargs ();2 //Txtargs.text = process.getcurrentprocess (). startinfo.arguments;3Txtargs.text + = args[0] +"\ r \ n";4 if(args. Length >1)5 {6Txtargs.text + = args[1];7}
Find the cause of the address: http://stackoverflow.com/questions/10682212/how-to-pass-argument-to-a-process