简体   繁体   English

尝试使用 Process.Start() 自动执行 exe 文件

[英]Trying to Use Process.Start() to automate executing an exe file

["

I am trying to Use Process.Start() to automate executing an exe file.<\/i>我正在尝试使用 Process.Start() 自动执行 exe 文件。<\/b><\/p>

I will first elaborate how to execute the exe file manually:<\/i>我将首先详细说明如何手动执行exe文件:<\/b><\/p>

first, load the exe file by double click it, and after loading, the terminal will show entry info and the last line, the string 'udec>' is the place to type in commands (please ignore the Chinese characters due to my OS).<\/i>首先,双击加载exe文件,加载后终端会显示入口信息和最后一行,字符串'udec>'是输入命令的地方(由于我的操作系统,请忽略中文字符) .<\/b><\/p>

--- module2d plugin DFNModule2D loaded.
--- module2d plugin GeometryModule2D loaded.
--- module2d plugin Convert loaded.


                        U D E C: VERSION 7.00
               赏屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯突
               ?     Universal Distinct Element Code       ?
               掏屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯凸
               ?Copyright (c):Itasca Consulting Group 2017 ?
               ?                                           ?
               韧屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯图
                Licensee:      Itasca Consulting Group, Inc.
                               Minneapolis, Minnesota  USA
                Options:
                               Barton-Bandis
                               Creep
                               CppUdm
                               Flow
                               Thermal


                Memory:          4096 MBytes
                Precision:     Double
udec>
["

It's not clear if StandardInput is ever exiting.<\/i>尚不清楚 StandardInput 是否曾经退出。<\/b> Also EnableRaisingEvents = true<\/code> should be specified.<\/i>还应指定EnableRaisingEvents = true<\/code> 。<\/b> It's not necessary to read the output in order to send input.<\/i>无需读取输出即可发送输入。<\/b> The code below shows how to provide input using StandardInput.<\/i>下面的代码显示了如何使用 StandardInput 提供输入。<\/b> In the code below, the first prompt will be i = 0<\/code> .<\/i>在下面的代码中,第一个提示将是i = 0<\/code> 。<\/b> If a second prompt occurs, this would be specified with i = 1<\/code> .<\/i>如果出现第二个提示,则使用i = 1<\/code>指定。<\/b><\/p>

Try the following:<\/i>尝试以下操作:<\/b><\/p>

Add the following using statements<\/strong> :<\/i>添加以下 using 语句<\/strong>:<\/b><\/p>

  • using System.IO;<\/code><\/li>
  • using System.Diagnostics;<\/code><\/li><\/ul>

    RunProcess<\/strong> :<\/i>运行过程<\/strong>:<\/b><\/p>

     private void RunProcess() { ProcessStartInfo startInfo = new ProcessStartInfo() { CreateNoWindow = true, FileName = @"D:\\Program Files\\ITASCA\\UDEC700\\Exe64\?onsole2017.exe", RedirectStandardError = true, RedirectStandardInput = true, RedirectStandardOutput = true, UseShellExecute = false, WindowStyle = ProcessWindowStyle.Hidden }; using (Process p = new Process() { StartInfo = startInfo, EnableRaisingEvents = true }) { \/\/subscribe to event and add event handler code p.ErrorDataReceived += (sender, e) => { if (!String.IsNullOrEmpty(e.Data)) { \/\/ToDo: add desired code Debug.WriteLine("Error: " + e.Data); } }; \/\/subscribe to event and add event handler code p.OutputDataReceived += (sender, e) => { if (!String.IsNullOrEmpty(e.Data)) { \/\/ToDo: add desired code Debug.WriteLine("Output: " + e.Data); } }; \/\/start p.Start(); p.BeginErrorReadLine(); \/\/begin async reading for standard error p.BeginOutputReadLine(); \/\/begin async reading for standard output using (StreamWriter sw = p.StandardInput) { \/\/provide values for each input prompt \/\/ToDo: add values for each input prompt - changing the for loop as necessary \/\/Note: Since we only have 1 prompt, using a loop is unnecessary - a single 'WriteLine' statement would suffice for (int i = 0; i < 1; i++) { \/\/if there are additional prompts add them below; else if (i = 1)... if (i == 0) sw.WriteLine(@"call 'D:\\Work\\202205\\20220525\\tunnel-for-cmd.txt'"); \/\/1st prompt else break; \/\/exit } } \/\/waits until the process is finished before continuing p.WaitForExit(); } }<\/code><\/pre>

    Resources<\/strong> :<\/i>资源<\/strong>:<\/b><\/p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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