通过OpenFileDialog浏览设备并通过FTP下载

时间:2018-10-30 14:36:37

标签: c# .net winforms ftp openfiledialog

我正在尝试通过FTP从设备下载一些文件。但是现在我对OpenFileDialog有一个问题。当我选择一个文件时,它首先开始缓存,这需要很长时间。我只想知道我在对话框中选择的文件,仅此而已。然后,通过WebClient下载。

这是一个代码段:

OpenFileDialog dialog = new OpenFileDialog();
dialog.InitialDirectory = ftpAddress;

DialogResult result = dialog.ShowDialog(this);

if(result != DialogResult.OK)
{
    return false;
}

string selectedLogFile = dialog.FileName;

WebClient webclient = new WebClient();
webclient.Credentials = new NetworkCredential(login, password);

webclient.DownloadFile(ftpAddress+ selectedLogFile, exportTo + selectedLogFile);

1 个答案:

答案 0 :(得分:1)

如果您在OpenFileDialog的FTP服务器上选择一个文件,它将实际上将文件下载到本地临时文件夹,并为您返回该临时文件的路径。就像您将HTTP URL粘贴到文件中的对话框一样。

无法使其与FTP路径一起使用。为此,您必须实现自己的自定义对话框。

在Windows 10中实际上甚至不建议浏览FTP服务器。