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
有帮助吗?

解决方案

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();

其他提示

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.

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top