1

I have a console application that runs .bat and .vbs files.

The method which starts these processes is as follows:

public void runProcess(string aPath,string aName,string aFiletype)
{
  string stInfoFileName;
  string stInfoArgs;

  if(aFiletype == "bat")
  {
    stInfoFileName = (@aPath + @aName);
    stInfoArgs = string.Empty;
  }
  else
  { //vbs
    stInfoFileName = @"cscript";
    stInfoArgs = "//B //Nologo " + aName;
  }

  this.aProcess.StartInfo.FileName = stInfoFileName;
  this.aProcess.StartInfo.Arguments =  stInfoArgs;
  this.aProcess.StartInfo.WorkingDirectory = @aPath;

  this.aProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
  this.aProcess.Start();

  Console.WriteLine("information passed from batch: ???");

  this.aProcess.WaitForExit(); //<-- Optional if you want program running until your script exit
  this.aProcess.Close();
}

The current .bat files which it is starting are used to send data over ftp (ftp, ftps, or sftp).

When the process is running all information is shown in the process windows e.g. an error might occur, the file is not transferred and a message detailing this is displayed in this "child" process window. Once the process has finished the "child" window disappears along with the messages.

Can I somehow return this useful information to my main console application window?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
whytheq
  • 34,466
  • 65
  • 172
  • 267

2 Answers2

3

Check process exit code (Process.ExitCode) and/or capture console output.

See Capturing console output from a .NET application (C#)


Though you should better use some native .NET SFTP/FTP library.

For SFTP, see SFTP Libraries for .NET

For FTP and FTPS, use can use the FtpWebRequest from .NET framework.


If you want all-in-one solution, I can humbly suggest you my WinSCP .NET assembly. With the assembly, you can have the same code for SFTP, FTP and FTPS. Internally, the assembly actually runs WinSCP console application. So it is doing the same thing you are trying to code yourself.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • 1
    thanks Martin - very strange coincidence indeed that you should answer - we have just started using your (+ contributors) app for some sftp. The client we were using has turned out to be rather "flakey". – whytheq Jun 17 '15 at 12:52
  • I'm relatively inexperienced using c# and ftp technology - seems like your assembly could simplify our processes by getting rid of the batch files etc - but, for someone with limited c# experience how complex is your assembly to use? – whytheq Jun 17 '15 at 20:06
  • 1
    Install the [NuGet package](http://winscp.net/eng/docs/library_install#nuget) in few clicks in VS and then it's just a few lines of code, [see example](http://winscp.net/eng/docs/library#csharp). – Martin Prikryl Jun 17 '15 at 20:12
  • I will give it a go sir - no harm in trying! – whytheq Jun 17 '15 at 20:13
3

If you set this.aProcess.StartInfo.UseShellExecute to false then the child process should use the existing console window.

For batch files, this change also means you'll need to explicitly invoke cmd /c to run them, in exactly the same way you're currently invoking cscript /B to run .vbs scripts.

Harry Johnston
  • 35,639
  • 6
  • 68
  • 158
  • I will need to amend this line of my code? `stInfoArgs = string.Empty;` – whytheq Jun 18 '15 at 08:42
  • Will I also will need to amend this line of my code? `stInfoFileName = (@aPath + @aName);` currently it is just a full pathway – whytheq Jun 18 '15 at 08:42
  • I've added a follow up question connected with my sad attempt at implementing your suggestion: http://stackoverflow.com/questions/30914435/redirected-batch-file-process-output-not-running – whytheq Jun 18 '15 at 11:45
  • - I did not need to change anything i.e. your suggestion to explicitly invoke `cmd /c` is not correct in my context. My final code is detailed here: http://stackoverflow.com/questions/30914435/redirected-batch-file-process-output-not-running#30914435 – whytheq Jun 18 '15 at 12:40