2

I am attempting to transition from VB script to C# with some legacy code that a colleague has. I cannot seem to determine how to transfer this line over. Any assistance would be helpful.

Set objShell = CreateObject("wscript.shell")
objShell.Run """C:\Program Files\Ipswitch\WS_FTP Professional\ftpscrpt.com"" -f P:\share\getfiles.scp", 1, True
Set objShell = Nothing
Esoteric Screen Name
  • 6,082
  • 4
  • 29
  • 38
steventnorris
  • 5,656
  • 23
  • 93
  • 174

3 Answers3

2

This is from some old note I dug up, but should work:

Process proc = new Process();

proc.StartInfo.FileName = @"C:\Program Files\Ipswitch\WS_FTP Professional\ftpscrpt.com";
proc.StartInfo.Arguments = @"-f P:\share\getfiles.scp";
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.UseShellExecute = false;
// start the process
proc.Start();
// wait for it to finish
proc.WaitForExit(5000);     
// get results
string output = proc.StandardOutput.ReadToEnd();
string error = proc.StandardError.ReadToEnd();
Jason
  • 3,844
  • 1
  • 21
  • 40
  • as the amount of wait time is guesswork, I'd prefer to wait indefinitely - `proc.WaitForExit(;`- or to explicitely with time-outs. – Ekkehard.Horner May 22 '13 at 15:16
  • @Ekkehard.Horner The amount to wait may sometimes be guesswork, but I'd assume there will be a reasonable amount of time it's expected to finish and if not, then some remediation may be necessary, waiting indefinitely has its merits, but I'd say it shouldn't be the norm. And of course, this is intended to help the op, not to be taken verbatim :) – Jason May 22 '13 at 15:27
  • This works well. I'm assuming the StandardError.ReadToEnd should be empty if the process completed without any issues? – steventnorris May 22 '13 at 16:33
1

I suppose the exact C# equivalent would be this:

var objShell = Microsoft.VisualBasic.Interaction.CreateObject("wscript.shell");
objShell.Run("\"C:\\Program Files\\Ipswitch\\WS_FTP Professional\\ftpscrpt.com\\" -f P:\share\getfiles.scp", 1, true);
objShell = null;

But really, you should just add a reference to the assembly in question and call its methods directly.

Douglas Barbin
  • 3,595
  • 2
  • 14
  • 34
1

See the example (Scroll down to examples) for information on executing the command. You can try to change the UseShellExecute option to get a close result.

thmshd
  • 5,729
  • 3
  • 39
  • 67