简体   繁体   English

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

[英]Automatically download files in FTP server

I don't have any knowledge about C# but I need to create an FTP server that automatically download a list of files (one by one, from the last to the first), so I downloaded some source codes of FTP servers and the most functional I found have a little problem, my task is to get a server that download files automatically, but the code I get open an window to select where to save the file. 我对C#没有任何了解,但是我需要创建一个FTP服务器来自动下载文件列表(从最后到第一个,一个一个地下载),因此我下载了一些FTP服务器的源代码,并且功能最强大我发现有一个小问题,我的任务是让服务器自动下载文件,但是我得到的代码打开一个窗口来选择保存文件的位置。

How can i change it to download the files automatically? 如何更改它以自动下载文件?

(If possible, explain throughly how your code work, it will help me to better understand and learn C#) (如果可能,请彻底解释您的代码如何工作,这将帮助我更好地理解和学习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();
}

If you want more details ask in the comments. 如果您需要更多详细信息,请在评论中提问。

(Sorry for bad speech, i'm not fluently in english) (对不起,我的英语不好。)

You need to remove the SaveFileDialog() it is meant to save file interactively. 您需要删除SaveFileDialog(),它旨在以交互方式保存文件。

  • Get the list of the file and put in array 获取文件列表并放入数组
  • and loop (foreach) the array to run the download method 并循环(foreach)数组以运行下载方法
  • better yet, run the method in a separate thread or async, so it will not block the main thread. 更好的是,在单独的线程或异步中运行该方法,因此它不会阻塞主线程。

Pseudo: 伪:

{run in thread

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM