1

I'm using the below code to log output of a cmd call to a file however it's not working at times.

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
//startInfo.Arguments = "/C dir C:\\  >c:\\temp\\dir.txt";
startInfo.Arguments = "/C \"C:\\Program Files\\Geth\\geth.exe\" --exec \"web3.eth.getBalance(web3.eth.accounts[0]);\" attach >c:\\temp\\out.txt";
process.StartInfo = startInfo;
process.Start();

The simple dir works fine. Using the Ethereum geth.exe without --exec works fine. However once I include the --exec argument the output is blank. Both commands work fine and produce output if manually called in cmd.exe.

"C:\Program Files\Geth\geth.exe" attach >c:\temp\out.txt

"C:\Program Files\Geth\geth.exe" --exec "web3.eth.getBalance(web3.eth.accounts[0]);" attach >c:\temp\out.txt

JD_
  • 23
  • 2
  • I gave up trying to figure it out and decided to just create a batch file in C#, and then call the batch file using cmd. The log file was created without issue. – JD_ Feb 06 '18 at 08:04

1 Answers1

0

I see you found a workaround, but for others:

process.Start();
process.WaitForExit();

You have to wait for the process to exit.

Mr. B
  • 2,845
  • 1
  • 21
  • 31