【问题标题】:Automatically download files in FTP server自动下载 FTP 服务器中的文件
【发布时间】:2016-02-09 11:00:35
【问题描述】:

我对C#一无所知,但是我需要创建一个FTP服务器,它会自动下载文件列表(一个一个,从最后一个到第一个),所以我下载了一些FTP服务器的源代码和我发现的最实用的有一个小问题,我的任务是让一个服务器自动下载文件,但是我得到的代码会打开一个窗口来选择保存文件的位置。

如何更改它以自动下载文件?

(如果可能,请详细解释您的代码是如何工作的,这将有助于我更好地理解和学习 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();
}

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

(对不起,我的英语不好,我的英语不太流利)

【问题讨论】:

    标签: c# ftp ftp-client


    【解决方案1】:

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

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

    伪:

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

    【讨论】:

    • 完美!通过一些编辑它解决了我的问题! (^-^)/
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多