2016-09-22 62 views
1

我正在嘗試在C#中編寫客戶端代碼,以使用WinSCP將1kb文件傳輸到服務器。以下代碼有效,但完整的過程大約需要11秒。該代碼需要10秒鐘來協商連接並對用戶進行身份驗證。實際的文件傳輸發生得非常快。提高WinSCP SCP連接時間

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

我的代碼是「整個C#示例」的一個編輯的版本從WinSCP賦予網站
https://winscp.net/eng/docs/guide_dotnet#csharp_example

沒有編輯服務器,有沒有什麼變化,我可以做,以減少傳輸時間?

回答

1

使用SFTP協議(sftp://)。 WinSCP中的SCP協議具有很長的連接時間,因爲WinSCP需要檢查和調整環境以實現自動化。無論如何,SCP協議已經過時。

如果您確實需要使用SCP出於某種原因(似乎並非如此),您可以嘗試禁用環境調整。

無論如何,請不要自行驅動winscp.exe二進制文件。改爲使用WinSCP .NET assembly。它可以爲您節省很多麻煩。即使the link你指出你自己的建議。