login about faq

I plan to call a batch file which calls sftpg3 (as described here) internally from a C# windows service. What is the best method to execute the batch file from the c# program? And is there anyway I could catch the exception, if there is error return from SSH?


asked Sep 11 '11 at 15:01

leemeng's gravatar image


converted to question Sep 11 '11 at 16:30

Roman's gravatar image

Roman ♦♦

Hi Leemeng,

I haven't done this in C#, but I suppose you could do it using something like this (source: msdn):

   // Start the child process.
   Process p = new Process();
   // Redirect the output stream of the child process.
   p.StartInfo.UseShellExecute = false;
   p.StartInfo.RedirectStandardOutput = true;
   p.StartInfo.FileName = "<path_to_binary>\sftpg3.exe";
   p.StartInfo.Arguments = "-B batchfile.txt user@server";
   // Do not wait for the child process to exit before
   // reading to the end of its redirected stream.
   // p.WaitForExit();
   // Read the output stream first and then wait.
   string output = p.StandardOutput.ReadToEnd();
   catch (Exception objException)
      // Do something

Please not that the above code is just a sample and completely untested

Also, if calling this from within a windows service, remember that the service must be running as the user account that has the server host keys already saved.


answered Sep 11 '11 at 16:41

Roman's gravatar image

Roman ♦♦

edited Sep 11 '11 at 16:41

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or __italic__
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported



Asked: Sep 11 '11 at 15:01

Seen: 6,194 times

Last updated: Sep 11 '11 at 16:41

All user contributed content licensed under the cc-by-sa license.
Powered by OSQA.