简体   繁体   English

从Windows窗体向运行控制台应用程序传递多个参数

[英]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: 我创建了两个过程:

  1. go to the file location 转到文件位置
  2. to execute application on file location 在文件位置执行应用程序

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 when UseShellExecute is true than when UseShellExecute is false . UseShellExecutetrue时, WorkingDirectory属性的行为与UseShellExecutefalse时的行为不同。 When UseShellExecute is true , the WorkingDirectory property specifies the location of the executable. UseShellExecutetrueWorkingDirectory属性指定可执行文件的位置。 If WorkingDirectory is an empty string, the current directory is understood to contain the executable. 如果WorkingDirectory是一个空字符串,则当前目录将被理解为包含可执行文件。

Note - When UseShellExecute is true , the working directory of the application that starts the executable is also the working directory of the executable. 注-当UseShellExecutetrue ,启动可执行文件的应用程序的工作目录也是可执行文件的工作目录。

When UseShellExecute is false , the WorkingDirectory property is not used to find the executable. 如果UseShellExecutefalse ,则不使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM