![](/img/trans.png)
[英]C# WinSCP .NET assembly Download the latest file from the latest directory
[英]Unable to download file into directory using WinSCP .NET assembly
我正在为SFTP功能实现WinSCP .NET程序集。 我能够建立会话,列出目录并将文件下载到\\bin\\x86\\Debug
,但是由于某种原因,我无法将文件下载到本地驱动器上的目录bin\\x86\\Debug\\edi
。
下载代码:
public static List<string> GetSFTP(string host, string user, string password, int port, string source, string dest, string remoteDest)
{
List<string> filenames = new List<string>();
System.IO.Directory.CreateDirectory(dest);
SessionOptions winSCPSessionOptions = new SessionOptions();
winSCPSessionOptions.HostName = host;
winSCPSessionOptions.Password = password;
winSCPSessionOptions.PortNumber = port;
winSCPSessionOptions.UserName = user;
winSCPSessionOptions.Protocol = WinSCP.Protocol.Sftp;
// This is a kind of dangerous setting, but as we do not have the host
// key fingerprints this allows us to connect without complaint.
winSCPSessionOptions.GiveUpSecurityAndAcceptAnySshHostKey = true;
Session session = new Session();
session.Open(winSCPSessionOptions);
RemoteDirectoryInfo remoteDirInfo = session.ListDirectory(remoteDest);
foreach (RemoteFileInfo fileInfo in remoteDirInfo.Files)
{
if (fileInfo.Name.Equals(".") || fileInfo.Name.Equals("..")) { continue; }
Console.WriteLine("{0}", remoteDest + fileInfo.Name);
try
{
TransferOperationResult result = session.GetFiles(remoteDest + fileInfo.Name, dest+fileInfo.Name);
if (!result.IsSuccess)
{
Console.WriteLine("Failed to download!");
foreach (SessionException ex in result.Failures)
{
Console.WriteLine(ex.ToString());
}
}
else
{
Console.WriteLine("Successfully downloaded file!");
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
filenames.Add(fileInfo.Name);
}
return filenames;
}
通过以下方式调用:
GetSFTP("sftp.server.com", "user", "password", 22, "/ProcessedFiles/", "edi/", "/ProcessedFiles/Processed/");
我试过了:
GetSFTP("sftp.server.com", "user", "password", 22, "/ProcessedFiles/", "/edi/", "/ProcessedFiles/Processed/");
GetSFTP("sftp.server.com", "user", "password", 22, "/ProcessedFiles/", "\\edi\\", "/ProcessedFiles/Processed/");
GetSFTP("sftp.server.com", "user", "password", 22, "/ProcessedFiles/", "edi\\", "/ProcessedFiles/Processed/");
在所有情况下,我都会得到%2Fedi%2Ffilename.txt
: %2Fedi%2Ffilename.txt
下载到我的工作目录中,而不是指定的路径中。
此代码应工作:
GetSFTP("sftp.server.com", "user", "password", 22, "/ProcessedFiles/", "edi\\", "/ProcessedFiles/Processed/");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.