C# start exe with parameters(2 methods) and close exe

Lionsure 2020-08-29 Original by the website

It is often necessary to use the main program to start and call another exe program in the process of Winforms software development. A typical example is software online update; the main program exe is used to start the update program exe. After the update file is downloaded, the update exe closes the main program exe, and then replace the main program exe. After the update is completed, the update program exe will start the main program exe.

One exe starts to call another exe, with or without parameters; if it takes parameters, the exe must not be running before starting another exe, otherwise it will fail. Let's take a look at how one exe starts another exe, and how one exe closes another exe.

 

I. C# start exe(i.e. C# call exe file)

1. C# start exe without parameters

Method 1: System.Diagnostics.Process.Start(exe path), for example, System.Diagnostics.Process.Start("D:\program\winform\update.exe")

, you can also write a relative path in the same solution.

 

Method 2:

using System.Diagnostics;

//Start exe with absolute path
       private void StartExe(string filePath)
       {
              Process proc = new Process();
              proc.StartInfo.UseShellExecute = true;//Whether to use the operating system shell to start the process

       proc.StartInfo.WorkingDirectory = filePath;//The initial directory of the startup process
              proc.Start();
       }

Call: StartExe("D:\program\winform\update.exe");

 

2. C# start exe with parameters

Method 1:

System.Diagnostics.Process.Start("D:\program\winform\update.exe", parm);

 

Received in the launched exe:

static void Main(string[] args)
       {
              if (args != null && args.Length == 1)
              string parm = args[0];
       }

 

Method 2:

using System;
       using System.Diagnostics;

//Start exe with absolute path
       private void StartExe(string filePath)
       {
              Process proc = new Process();
              proc.StartInfo.UseShellExecute = true;//Whether to use the operating system shell to start the process

       proc.StartInfo.WorkingDirectory = filePath;//The initial directory of the startup process
              proc.StartInfo.Arguments = arg1 + " " + arg2;//Parameters passed
              proc.Start();
       }

 

Received in the launched exe:

private void Form1_Load(object sender, EventArgs e)
       {
              string[] arrArgs = Environment.GetCommandLineArgs();
              if (arrArgs.Length > 1)
              {
                     string arg0 = arrArgs[0].ToString();//The 0th parameter is its own path
                     string arg1 = arrArgs[1].ToString();
                     string arg2 = arrArgs[2].ToString();
              }
       }

 

 

II. C# close exe

//exeName is the name of the exe process to be closed
       private void CloseExe(string exeName)
       {
              Process[] arrPro = Process.GetProcessesByName(exeName);
              foreach (Process pro in arrPro)
                     pro.Kill();
       }

Call: CloseExe("update")