简体   繁体   English

在 SftpClient 中,如何检查文件是否已在目录中下载?

[英]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.

相关问题 如何检查是否已使用NSubstitute调用匿名函数? - How can I check if an anonymous function has been called with NSubstitute? 如何检查是否已抛出任何异常? - How can I check if any exception has already been thrown? 如何在.NET中检查是否已订阅某个事件? - How can I check if an event has been subscribed to, in .NET? 如何检查文件是否已复制到 output 目录,因为它比以前的文件更新? - How to check if file has been copied to output directory because it was newer than one previously? 如何查看远程Web服务器上的文件是否已更新? - How can I see if a file on a remote webserver has been updated? 如何删除已在webbrowser控件中导航的文件? - How can i delete the file that has been navigated in a webbrowser control? 如何检查表单是否已打开,如果已打开,是否从控件中获取值? - How can I check if a form has been opened, and if it has, get the value from the control? 如何仅在XtraInputBox中接受数字值,如何检查单击了哪个按钮? - How to accept only numeric value in XtraInputBox and how can I check which button has been clicked? 在序列化之前,如何检查类文件是否已更改? - How do I check if a class file has been changed before serializing it? 如何使用C#检查使用最多的程序? - How can I check what program has been used the most using C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM