[英]In SftpClient how can i check if the file has been donwlonded in directory or not?
I am downloading from SFTP and want to check if the file exists in the folder "directory @"D:..." or not我正在从 SFTP 下载并想检查该文件是否存在于文件夹“目录@“D:...”中
using (SftpClient sftp = new SftpClient(Host, Port, Username, Password))
{
sftp.Connect();
var files = sftp.ListDirectory(RemoteFileName);
string downloadFileNames = string.Empty;
foreach (var file in files)
{
if (file.FullName.EndsWith(".gz"))
{
using (Stream fileStream = File.Create(Path.Combine(directory, file.Name)))
{
sftp.DownloadFile(file.FullName, fileStream);
}
}
downloadFileNames += file.Name;
}
}
Based on your comment:根据您的评论:
i don't want evey time to download the same files i want to check if its arleady downloaded then not download it again我不想每次都下载相同的文件我想检查它是否已经下载然后不再下载
I am assuming that you want to only download files that you have not previously downloaded.我假设您只想下载以前未下载的文件。
In order to achieve this, use File.Exists
to verify whether you already downloaded the file to a location:为了实现这一点,请使用File.Exists
来验证您是否已将文件下载到某个位置:
foreach (var file in files)
{
if (file.FullName.EndsWith(".gz"))
{
var targetFilename = Path.Combine(directory, file.Name);
if (!File.Exists(targetFilename))
{
using (Stream fileStream = File.Create(targetFilename))
{
sftp.DownloadFile(file.FullName, fileStream);
}
downloadFileNames += file.Name;
}
}
}
This verifies whether the target file exists before downloading it from the SFTP server.这会在从 SFTP 服务器下载目标文件之前验证目标文件是否存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.