WPF uses the Process.Start () method to start an EXE file under a specified path and pass parameter receive parameters
A requirement was encountered in development: WPF program A starts WPF program B and passes parameters, and WPF program B receives parameters and processes them.
How to achieve such a demand, there are already a lot of articles on the network to describe the problem, here to make a record.
First, start WPF client B with the following code:
Private void Actionstartclientexcute () { var"exe location "; New Process (); = address; " arg1 arg2 " ; true ; Process. Start (); }
Add the following code to the receiving client:
Define a Mainapp class, which must have a main (string[] args) function with parameters, and set the Mainapp class with the main function as the Startup object.
Select the current project, and then right-click the properties----application panel, set the Startup object.
Externally passed parameters are in string[] args, for example, the passed parameter is the string "arg1 arg2", then the content of args is args[0]= "arg1", args[1]= " Arg2 ", where each array element is separated by a space.
There should be a App.xaml and a App.xaml.cs file in the project. They inherit to the application class, set the program's start page in App.xaml startupuri= "Window.xaml", which is our original program interface.
Here we need to instantiate the app class at the end of the main function: The Initapp method in the code.
Public classMainapp {[STAThread] Public Static voidMain (string[] args) { if(Args! =NULL&& args. Length = =2) { stringarg1= args[0]. Trim (); stringArg2= args[1]. Trim (); } Initapp (); } Public Static voidInitapp () {app app=NewApp (); App. InitializeComponent (); App. Run (); } }
This will enable WPF program A to open WPF program B and pass parameters to this requirement!!!
WPF uses the Process.Start () method to start an EXE file under a specified path and pass parameter receive parameters