WPF uses the Process.Start () method to start an EXE file under a specified path and pass parameter receive parameters

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.