ProcessStartInfo 启动bat并传参数
时间: 2024-05-10 18:18:02 浏览: 13
可以使用 `ProcessStartInfo` 类来启动一个 `bat` 文件,并传递参数。以下是一个示例代码:
```csharp
string fileName = "test.bat";
string arguments = "arg1 arg2 arg3";
ProcessStartInfo psi = new ProcessStartInfo(fileName, arguments);
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
Process process = new Process();
process.StartInfo = psi;
process.Start();
```
其中,`fileName` 是要启动的 `bat` 文件名,`arguments` 是要传递的参数,多个参数之间用空格隔开。
注意,`UseShellExecute` 必须设置为 `false`,否则无法传递参数。此外,如果不希望在启动 `bat` 文件时显示窗口,可以将 `CreateNoWindow` 设置为 `true`。
相关问题
c# 路径空格---ProcessStartInfo参数问题
你好!关于 C# 中路径空格的问题,可以在传递路径参数时使用引号将路径包裹起来,这样可以避免空格造成的影响。例如:
```csharp
string path = @"C:\Program Files\MyApp\app.exe";
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = path;
startInfo.Arguments = "\"C:\\My Folder\\my file.txt\"";
```
在上面的代码中,`startInfo.Arguments` 参数中的路径被用引号包裹起来,即 `"C:\My Folder\my file.txt"`,这样就可以正确传递包含空格的路径参数了。
c# 调用exe并传入参数
可以使用 Process 类来调用 exe 并传入参数。下面是一个简单的示例代码:
```csharp
using System.Diagnostics;
// exe 文件路径
string exePath = "C:\\example.exe";
// 参数
string arg1 = "argument1";
string arg2 = "argument2";
// 创建 ProcessStartInfo 对象来设置 exe 文件路径和参数
ProcessStartInfo startInfo = new ProcessStartInfo(exePath);
startInfo.Arguments = $"{arg1} {arg2}";
// 启动进程
Process.Start(startInfo);
```
在上面的示例代码中,我们首先指定 exe 文件路径,然后定义两个参数 arg1 和 arg2。接着,我们创建一个 ProcessStartInfo 对象并设置 exe 文件路径和参数,并将其传递给 Process.Start 方法来启动进程。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)