c#启动exe 不用子进程的方式启动
时间: 2023-07-15 11:14:41 浏览: 81
可以使用System.Diagnostics.Process类来启动exe文件,不必创建子进程。以下是示例代码:
```csharp
// 设置启动信息
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "your_file_path.exe";
// 可选设置参数
psi.Arguments = "arg1 arg2";
// 创建进程并启动
Process process = new Process();
process.StartInfo = psi;
process.Start();
```
需要注意的是,这种方式启动的exe文件会在当前进程中运行,如果你希望程序能够一直运行,那么需要在你的程序中添加相应的代码进行处理。
相关问题
c#启动exe 非子进程
可以使用C#中的Process类来启动一个外部的可执行文件(.exe文件),并且不作为当前进程的子进程运行。以下是一个简单的示例代码:
```csharp
using System.Diagnostics;
// ...
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "path/to/your/executable.exe";
startInfo.UseShellExecute = false;
Process process = Process.Start(startInfo);
```
在上面的代码中,我们首先创建了一个ProcessStartInfo对象,然后设置了要启动的可执行文件的路径和一些其他选项。其中,UseShellExecute属性设置为false,这将禁用使用操作系统的 shell 来启动进程。
然后,我们调用Process类的静态方法Start,传递我们刚刚创建的ProcessStartInfo对象作为参数。这将启动一个新的进程来运行我们指定的可执行文件。
请注意,这将启动一个全新的进程,并且不会作为当前进程的子进程运行,因此您不能直接与该进程通信。如果需要与该进程进行通信,请考虑使用进程间通信(IPC)技术,例如命名管道、共享内存等。
c#启动exe 5种方式
在C#中启动一个exe文件有多种方式,以下是其中五种常见的方式:
1. 使用Process.Start()方法启动exe文件。这是最常见的方法,可以在代码中使用以下语句来启动一个exe文件:
```
Process.Start("path/to/exe/file.exe");
```
2. 使用Process类的构造函数创建一个新的进程对象,并设置StartInfo属性来指定exe文件路径和其他启动选项。以下是一个示例代码:
```
Process process = new Process();
process.StartInfo.FileName = "path/to/exe/file.exe";
process.StartInfo.Arguments = "arg1 arg2";
process.Start();
```
3. 使用Shell类的静态方法启动exe文件。以下是一个示例代码:
```
ShellExecute(IntPtr.Zero, "open", "path/to/exe/file.exe", "", "", ShowCommands.SW_SHOWNORMAL);
```
4. 使用WMI(Windows Management Instrumentation)启动exe文件。以下是一个示例代码:
```
ManagementClass processClass = new ManagementClass("Win32_Process");
object[] methodArgs = { "path/to/exe/file.exe" };
processClass.InvokeMethod("Create", methodArgs);
```
5. 使用AppDomain类的ExecuteAssembly方法运行exe文件。以下是一个示例代码:
```
AppDomain.CurrentDomain.ExecuteAssembly("path/to/exe/file.exe");
```
以上是启动exe文件的五种常见方式,可以根据需要选择适合的方法来启动exe文件。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)