自动下载 FTP 服务器中的文件

Automatically download files in FTP server

我对 C# 没有任何了解,但我需要创建一个 FTP 服务器自动下载文件列表(一个接一个,从最后一个到第一个),所以我下载了一些FTP 服务器的源代码和我发现的最实用的源代码有点问题,我的任务是获得一个自动下载文件的服务器,但是我得到的代码打开了 window 到 select保存文件的位置。

如何更改为自动下载文件?

(如果可能的话,把你的代码是如何工作的解释清楚,这将有助于我更好地理解和学习C#)

private void ServerFileListView_DockChanged(object sender, EventArgs e)
{
    foreach (ListViewItem item in ServerFileListView.Items)
    {
      item.Selected = true;
    }

    byte[] file;

    Server.Download(MachineInfo.GetJustIP(), ServerFileListView.SelectedItems[0].SubItems[2].Text, out file);

    SaveFileDialog save = new SaveFileDialog();
    save.Title = "It saves the downloaded file.";
    save.SupportMultiDottedExtensions = false;
    save.Filter = "*.png|*.png";
    save.FileName = ServerFileListView.SelectedItems[0].SubItems[2].Text;
    if (save.ShowDialog() != System.Windows.Forms.DialogResult.Cancel)
    {
        System.IO.File.WriteAllBytes(save.FileName, file);
        MessageBox.Show(ServerFileListView.SelectedItems[0].SubItems[2].Text +" has been downloaded.", "FTP File Sharing", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    save.Dispose();
}

如果您想了解更多详情,请在评论中询问。

(对不起,我的英语不好)

您需要删除用于交互式保存文件的 SaveFileDialog()。

  • 获取文件列表并放入数组
  • 并将数组循环 (foreach) 到 运行 下载方法
  • 更好的是,运行该方法在单独的线程或异步中,因此它不会阻塞主线程。

伪:

{run in thread

    if(ftp is connected)
    {
       connect;
       string listToDownload[] = getListFileFromServer;
        foreach (var item from listToDownload)
         {file = getFileFromServer;
          saveToDisk(file);
          }
    }
}