I'm currently trying to write client code in C# to transfer a 1kb file to a server using WinSCP. The following code works but the complete procedure takes approx 11 seconds. The code takes 10 seconds to negotiate the connection and authenticate the user. The actual file transfer happens very fast.
Process winscp = new Process();
winscp.StartInfo.FileName = "winscp.com";
winscp.StartInfo.Arguments = "/xmllog=\"" + "sftp.txt" + "\"";
winscp.StartInfo.UseShellExecute = false;
winscp.StartInfo.RedirectStandardInput = true;
winscp.StartInfo.RedirectStandardOutput = true;
winscp.StartInfo.CreateNoWindow = true;
winscp.Start();
// Feed in the scripting commands
winscp.StandardInput.WriteLine("option batch abort");
winscp.StandardInput.WriteLine("option confirm off");
List<string> FtpCommands = new List<string>
{
string.Format("open scp://{0}:{1}@{2}:22",CONNECTION_SFTP_USER,CONNECTION_SFTP_PASSWORD, CONNECTION_SFTP_IP),
string.Format("put \"{0}\" /home/pi/progs/programs/{1}", file, program),
"exit"
};
LogMessage("Sending ftp commands");
foreach (var ftpCommand in FtpCommands)
{
LogMessage(ftpCommand);
winscp.StandardInput.WriteLine(ftpCommand);
}
winscp.StandardInput.Close();
// Collect all output (not used in this example)
string output = winscp.StandardOutput.ReadToEnd();
// Wait until WinSCP finishes
winscp.WaitForExit();
My code is an edited version of the "Full C# example" from the WinSCP website
https://winscp.net/eng/docs/guide_dotnet#csharp_example
Without editing the server, is there any changes I could make to reduce the transfer time?
Use the SFTP protocol (sftp://
). The SCP protocol in WinSCP has long connection time, as WinSCP needs to check and adjust an environment to be automation-friendly. And the SCP protocol is obsolete anyway.
If you really need to use SCP for some reason (does not seem to be the case), you can try to disable the environment adjusting.
In any case, do not drive winscp.exe
binary yourself. Use the WinSCP .NET assembly instead. It saves you lot of hassle. Even the link you point to yourself suggests that.