改善 WinSCP SCP 连接时间

Improve WinSCP SCP connection time

我目前正在尝试使用 C# 编写客户端代码,以使用 WinSCP 将 1kb 文件传输到服务器。以下代码有效,但完整的过程大约需要 11 秒。该代码需要 10 秒来协商连接和验证用户。实际的文件传输速度非常快。

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

我的代码是来自 WinSCP 网站
的"Full C# example"的编辑版本 https://winscp.net/eng/docs/guide_dotnet#csharp_example

在不编辑服务器的情况下,是否可以进行任何更改以减少传输时间?

使用 SFTP 协议 (sftp://)。 WinSCP 中的 SCP 协议具有较长的连接时间,因为 WinSCP 需要检查和调整环境以实现自动化。而且 SCP 协议已经过时了。

如果你真的因为某些原因需要使用SCP(好像不是这样),你可以尝试禁用环境调整。

无论如何,不​​要自己驱动winscp.exe二进制文件。使用 WinSCP .NET assembly instead. It saves you lot of hassle. Even the link 你指向自己的建议。