user2199192
user2199192

Reputation: 175

Running an exe from with another exe C# and closing it

I am trying to run an exe file inside another exe file in C#

That works well, however my problem is the exe that should run inside the other opens another console window, which I need to press "Enter" in, for it to stop after it has done what it does.

Is there a way to do that?

This is the code I have so far.

var proc = new Process();
proc.StartInfo.FileName = "thefile.exe";
proc.Start();
proc.WaitForExit();
var exitCode = proc.ExitCode;
proc.Close();

Thanks

Upvotes: 2

Views: 939

Answers (2)

Alexander Wilhelm
Alexander Wilhelm

Reputation: 58

Just use cmd to open and terminat the exe / user window.

string executingCommand;
executingCommand= "/C Path\\To\\Your\\.exe";    // the /C terminates after carrying out the command
System.Diagnostics.Process.Start("CMD.exe", executingCommand);

Upvotes: 2

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

Are looking for the input redirectioning?

https://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardinput(v=vs.110).aspx

  int exitCode = -1;

  // Put IDisposable into using
  using (var proc = new Process()) {
    proc.StartInfo.FileName = "thefile.exe";
    proc.StartInfo.RedirectStandardInput = true;

    proc.Start();

    using (StreamWriter inputWriter = proc.StandardInput) {
      inputWriter.Write('\n');
    }

    proc.WaitForExit();
    exitCode = proc.ExitCode;
  }

Upvotes: 1

Related Questions