views:

382

answers:

1

I have a console app and a win forms app that both need to call out to a remote server for some data, they make a call to the command line part of Putty, plink.exe, to run a remote command over SSH.

I created a tiny class library for both to share, running the following:

public static string RunCommand(string command, string arguments) {
  ProcessStartInfo startInfo = new ProcessStartInfo {
      FileName = command,
      Arguments = arguments,
      UseShellExecute = false,
      CreateNoWindow = true,
      RedirectStandardOutput = true
  };
  string output = null;
  using (Process p = new Process()) {
      p.StartInfo = processStartInfo;
      p.Start();
      output = p.StandardOutput.ReadToEnd();
      p.WaitForExit();
  }
  return output;
}

Under the console application everything works fine, under the win forms it doesn't error, it seems that WaitForExit() just doesn't wait. I get an empty string for output. I've confirmed from the remote server the user logged in, so it seems the command has run.

Any ideas?

+2  A: 

Under Windows Console applications have STDIN, STDOUT, and STDERR. Windowed applications do not. When you create a process under a Console application the STDIN etc. are inherited by the child application. This does not happen in the Windowed application.

The RedirectStandardInput=true works because it makes the system create a Writer for the STDIN that you can use to send input to the child process. In your case the child doesn't need the input it just needs the presence of the input. YMMV.

James Keesey