Search code examples
c#.netwinformsftpopenfiledialog

Browse device via OpenFileDialog and download via FTP


I'm trying to download some files via FTP from a device. But now I have one problem with the OpenFileDialog. When I'm selecting one file, it starts caching first and that takes a long time. I just want to know which file I'm choosing in the dialog, nothing else. Then, download it via WebClient.

Here's a code snippet:

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);

Solution

  • If you select a file on FTP server in OpenFileDialog, it will actually download the file to a local temporary folder and return you a path to that temporary file. The same way, as if you have pasted an HTTP URL to a file into the dialog.

    There's no way to make it work with FTP paths. For that you have to implement your own custom dialog.

    And actually even browsing an FTP server was deprecated in Windows 10 anyway.