【发布时间】:2020-06-03 22:52:09
【问题描述】:
我正在使用 SSH.NET 库从 SFTP 服务器下载文件。当我给它完整的文件名时,它可以工作。但我想下载一个带有前缀名称的文件,在该文件夹中,前缀名称是POS_ETH_SE7*。总会有一个文件。下载后,我将其移至另一个文件夹。这是我的方法:
var auth = new PasswordAuthenticationMethod(username, password);
var connectionInfo = new ConnectionInfo(ipAddress, port, auth);
// Upload File
using (var sftp = new SftpClient(connectionInfo))
{
string pathLocalFile =
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
"POS_ETH_SE7.ics");
sftp.Connect();
Console.WriteLine("Downloading {0}", remoteFilePath);
using (Stream fileStream = File.OpenWrite(pathLocalFile))
using (StreamWriter writer = new StreamWriter(fileStream))
{
try
{
sftp.DownloadFile(remoteFilePath, fileStream);
}
catch (SftpPathNotFoundException ex)
{
}
}
try
{
var inFile = sftp.Get(remoteFilePath);
inFile.MoveTo(remoteMoveFileToPath + "/POS_ETH_SE7.xml");
}
catch (SftpPathNotFoundException ex)
{
Console.WriteLine("\nnothing to update...\n");
}
sftp.Disconnect();
}
【问题讨论】: