将多个参数从 Windows 窗体传递到正在运行的控制台应用程序



我有控制台应用程序,我们将其命名为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

WorkingDirectory属性在trueUseShellExecute时的行为与UseShellExecutefalse时的行为不同。trueUseShellExecute时,WorkingDirectory属性指定可执行文件的位置。如果WorkingDirectory为空字符串,则当前目录被理解为包含可执行文件。

注意 - 当UseShellExecutetrue时,启动可执行文件的应用程序的工作目录也是可执行文件的工作目录。

UseShellExecutefalse时,WorkingDirectory属性不用于查找可执行文件。相反,它的值适用于已启动的进程,并且仅在新进程的上下文中有意义。

例如

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);
}
}

相关内容

最新更新