从 windows 表单向 运行 控制台应用程序传递多个参数

Passing multiple arguments to running console application from windows form

我有控制台应用程序,我们将其命名为 X.exe。它使用两个参数让我们说 'a' 和 'a.tmp' 其中 a 是我的输入文件名 a.tmp 是输出文件名。在控制台上,我通常 运行 应用程序如下: X a a.tmp 但首先我必须出现在输入文件的位置 'a' 否则,如果我尝试提供其绝对路径,应用程序将无法运行。 我已经创建了 windows 表单到 运行 这些控制台应用程序,但正如我之前所说,应用程序必须在文件位置启动。 我尝试使用流程对象,但应用程序无法正常工作。 我创建了两个进程:

  1. 转到文件位置
  2. 在文件上执行应用程序 位置

Question: can I excute these multiple commands in one go and avoid using IPC?

你可以使用的是ProcessStartInfo.WorkingDirectory

例如来自 MS Docs - ProcessStartInfo Class

The WorkingDirectory property behaves differently when UseShellExecute is true than when UseShellExecute is false. When UseShellExecute is true, the WorkingDirectory property specifies the location of the executable. If WorkingDirectory is an empty string, the current directory is understood to contain the executable.

Note - When UseShellExecute is true, the working directory of the application that starts the executable is also the working directory of the executable.

When UseShellExecute is false, the WorkingDirectory property is not used to find the executable. Instead, its value applies to the process that is started and only has meaning within the context of the new process.

例如

    public static void Main()
    {
        Process myProcess = new Process();

        try
        {                
            myProcess.StartInfo.UseShellExecute = true;

            // You can start any process, HelloWorld is a do-nothing example.
            myProcess.StartInfo.FileName = "X.exe"; /
            myProcess.WorkingDirectory = "C:\SomeDirectory That contains A and A.tmp"
            myProcess.Start();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }