I am trying to Use Process.Start() to automate executing an exe file.
I will first elaborate how to execute the exe file manually:
first, load the exe file by double click it, and after loading, the terminal will show entry info as follows, and the last line, the string '
udec>' is the place to type in commands (please ignore the Chinese characters due to my OS).
--- 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>
then I will type in command call 'D:\Work\202205\20220525\tunnel-for-cmd.txt' to execute it, and the result is as follows:
udec>call 'D:\Work\202205\20220525\tunnel-for-cmd.txt'
................
................
calculation results
................
................
What I have tried:
What I am trying is to automate this activity, I use while loop and read from StandardOutput by int v = p.StandardOutput.Read();, and if v == 62 (which means I reached the end of the output, the '>' character), I will then write my command line to standard input. and my code is as follows:
try
{
var psi = new ProcessStartInfo()
{
UseShellExecute = false,
FileName = @"D:\Program Files\ITASCA\UDEC700\Exe64\udecConsole2017.exe",
RedirectStandardInput = true,
RedirectStandardOutput = true,
CreateNoWindow = true,
};
var p = System.Diagnostics.Process.Start(psi);
while (true)
{
int v = p.StandardOutput.Read();
Console.Write((char)v);
if (v == 62)
{
break;
}
if (v == -1)
break;
}
Console.Write(@"call 'D:\Work\202205\20220525\tunnel-for-cmd.txt'");
p.StandardInput.WriteLine(@"call 'D:\Work\202205\20220525\tunnel-for-cmd.txt'");
p.StandardInput.Flush();
string content = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Console.WriteLine(content);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
If I run the above code, I will get the starting infomation as expected (shown in the first snippet). Howerver, my simulation of "call 'D:\Work\202205\20220525\tunnel-for-cmd.txt'" fails. Although p.StandardInput.WriteLine() can be executed, when code runs after line 44 (string content = p.StandardOutput.ReadToEnd();), the program got hang up and deadlocked, and does not response so that I can only kill this progam.
I am not sure how to solve it.