Execute application in C#
Question: How to launch an executable file (.EXE) using C# code?
Answer:
The following C# code demonstrates how to start an application in a C# program:
<code class="language-csharp">using System.Diagnostics; // 准备要运行的进程 ProcessStartInfo start = new ProcessStartInfo(); // 输入命令行参数,即你在可执行文件名之后输入的所有内容 start.Arguments = arguments; // 输入要运行的可执行文件,包括完整路径 start.FileName = ExeName; // 是否显示控制台窗口? start.WindowStyle = ProcessWindowStyle.Hidden; start.CreateNoWindow = true; int exitCode; // 运行外部进程并等待其完成 using (Process proc = Process.Start(start)) { proc.WaitForExit(); // 获取应用程序的退出代码 exitCode = proc.ExitCode; }</code>
In this code, first create a ProcessStartInfo object to specify the executable file and its parameters. You can set other options, such as whether to display a console window or create a new process window.
Next, create a Process object to start the executable and wait for it to complete. The WaitForExit() method blocks until the process exits.
Finally, use the ExitCode property of the Process object to retrieve the exit code of the started application. This value indicates whether the application executed successfully.
This code provides a flexible way to launch an application and control its behavior from C# code.
The above is the detailed content of How to Launch an Executable (.EXE) File in C#?. For more information, please follow other related articles on the PHP Chinese website!