[英]Passing multiple arguments to running console application from windows form
I have the console application, lets name it X.exe . 我有控制台应用程序,将其命名为X.exe 。 It works with two arguments lets say 'a' and 'a.tmp' where a is my input file name and a.tmp is output filename .
它与两个参数一起工作,比如说'a'和'a.tmp',其中a 是我的输入文件名 , a.tmp 是输出filename 。 On console I usually run application like: X a a.tmp but first I have to be present in location of input file 'a' else application wont work if I tried to give its absolute path.
在控制台上,我通常运行如下应用程序: X a a.tmp, 但首先我必须在输入文件'a'的位置出现,否则,如果我尝试给出其绝对路径,则应用程序将无法工作。 I have created windows form to run these console application, but as I said earlier application must be started in file location.
我已经创建了Windows窗体来运行这些控制台应用程序,但是正如我之前所说,必须在文件位置启动该应用程序。 I tried using process object but application was not working.
我尝试使用过程对象,但应用程序无法正常工作。 I created two processes:
我创建了两个过程:
Question: can I excute these multiple commands in one go and avoid using IPC?
What you can use is the ProcessStartInfo.WorkingDirectory
. 您可以使用
ProcessStartInfo.WorkingDirectory
。
Eg from MS Docs - ProcessStartInfo Class 例如,来自MS Docs-ProcessStartInfo类
The
WorkingDirectory
property behaves differently whenUseShellExecute
istrue
than whenUseShellExecute
isfalse
.当
UseShellExecute
为true
时,WorkingDirectory
属性的行为与UseShellExecute
为false
时的行为不同。 WhenUseShellExecute
istrue
, theWorkingDirectory
property specifies the location of the executable.当
UseShellExecute
为true
,WorkingDirectory
属性指定可执行文件的位置。 IfWorkingDirectory
is an empty string, the current directory is understood to contain the executable.如果
WorkingDirectory
是一个空字符串,则当前目录将被理解为包含可执行文件。Note - When
UseShellExecute
istrue
, the working directory of the application that starts the executable is also the working directory of the executable.注-当
UseShellExecute
为true
,启动可执行文件的应用程序的工作目录也是可执行文件的工作目录。When
UseShellExecute
isfalse
, theWorkingDirectory
property is not used to find the executable.如果
UseShellExecute
为false
,则不使用WorkingDirectory
属性查找可执行文件。 Instead, its value applies to the process that is started and only has meaning within the context of the new process.相反,它的值适用于已启动的流程,并且仅在新流程的上下文中具有含义。
Eg 例如
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.