c#启动exe 非子进程
时间: 2023-07-16 14:14:31 浏览: 129
要启动一个外部的 exe 文件,可以使用 .NET Framework 提供的 Process 类。在 Process 类中,有一个静态方法 Start,可以启动一个外部的应用程序。在启动外部应用程序时,可以指定一些参数,如应用程序的路径、命令行参数等。
以下是一个简单的示例代码,演示如何使用 Process 类启动一个外部应用程序:
```csharp
using System.Diagnostics;
class Program
{
static void Main(string[] args)
{
string exePath = "C:\\MyApp\\MyApp.exe";
Process.Start(exePath);
}
}
```
在上面的示例代码中,我们定义了一个字符串变量 `exePath`,用来存放外部应用程序的路径。然后,我们通过 Process.Start 方法启动外部应用程序。这个方法会返回一个 Process 对象,我们可以使用这个对象来控制外部应用程序的行为。例如,我们可以等待应用程序退出,或者向应用程序发送消息等。
需要注意的是,使用 Process.Start 方法启动外部应用程序时,默认情况下会启动一个子进程。如果要启动一个独立的进程,可以设置 ProcessStartInfo 类的 UseShellExecute 属性为 false,然后使用该类作为参数调用 Process.Start 方法,示例代码如下:
```csharp
using System.Diagnostics;
class Program
{
static void Main(string[] args)
{
string exePath = "C:\\MyApp\\MyApp.exe";
ProcessStartInfo psi = new ProcessStartInfo(exePath);
psi.UseShellExecute = false;
Process.Start(psi);
}
}
```
在上面的示例代码中,我们创建了一个 ProcessStartInfo 对象,并将 UseShellExecute 属性设置为 false。然后,我们使用该对象作为参数调用 Process.Start 方法,从而启动一个独立的进程。