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?

Thanks.

asked Sep 11 '11 at 15:01

leemeng's gravatar image

leemeng
1223

converted to question Sep 11 '11 at 16:30

Roman's gravatar image

Roman ♦♦
7735817


Hi Leemeng,

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

try
   {
   // 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";
   p.Start();
   // 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();
   p.WaitForExit();
   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.

link

answered Sep 11 '11 at 16:41

Roman's gravatar image

Roman ♦♦
7735817

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

By RSS:

Answers

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

Tags:

×45
×5
×3

Asked: Sep 11 '11 at 15:01

Seen: 3,712 times

Last updated: Sep 11 '11 at 16:41

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