Bastien Vandamme
Bastien Vandamme

Reputation: 18485

Call a C# application from an existing C# application with ProcessStartInfo?

I have a solution that contains 2 projects:

xxxMode2 can be built as an independent desktop application. xxxTable project is a launcher. I will call xxxMode2 from xxxTable.

enter image description here

Here are 2 samples of code that I use to call the project xxxMode2 from the project xxxTable. Let's call them A and B.

        // Code A
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            Xxxxx.MainWindow mnw = new Xxxxx.MainWindow();
            mnw.Owner = this;
            mnw.ShowDialog();
        }
        // Code B
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            string currentFolder = Directory.GetCurrentDirectory();
            ProcessStartInfo pInfo = new ProcessStartInfo("xxx.xxx.xx.exe");
            pInfo.WorkingDirectory = currentFolder;            
            Process p = Process.Start(pInfo);
        }

In C#, what are the difference between calling a sub-project from a main-project between using the Classic way (code A) and the Process way (code B)?

I use this ProcessStartInfo() method when I want to launch an exe extern to my code. Is it correct, or make it sense, to do it from my main-project to my sub-project?

Upvotes: 0

Views: 103

Answers (1)

66Gramms
66Gramms

Reputation: 743

The difference is that when you go with the A method you have full control over that dialog. It is just going to be a window in the same process.

With method B however, you are making a child process, which means you can't access the variables and other data of the program you just started with the Process class. This way you open a new process.

You can still communicate with your program if you use method B, you can use the standard streams for that.

If you can most of the time it is preferable to go with method A, if for some reason you can't then method B is fine. This is really depending on the project and what you want to achieve, but method A should be almost always the way to go.

Upvotes: 2

Related Questions