VB.Net Asynchronous FTP Download Multiple Files Error RRS feed

  • Question

  • I'm creating a Console Application to automatically download several thousands of files from an FTP site.  I'm trying to use a WebClient to do this.  However, it works when I'm using synchronous function.  Now I'm trying to convert it into an Asynchronous function but I'm getting an error: "Expression does not produce a value".  Following is my Code (I'm just using an enum for list of stations but in actual work, there is going to be more than 50,000 files to download.). 

    Appreciate any help.  Thanks.

    Imports System.Net
    Imports System.Text
    Imports System.IO

    Module Module1

    Sub Main()
            For Each station In EnumToList(Of StationEnum)()
                Dim ftpFile As String = "" & station.ToString() & ".dly"
                Dim ftpUri As New Uri(ftpFile)
                Dim myfile As String = "D:\ProjectsWorkspaces\Weather 2018\from web\" & station.ToString() & ".dly"

                DownloadFileAsync(ftpUri, myfile)
        End Sub

        Public Async Function DownloadFileAsync(ftpUri As Uri, myfile As String) As Task(Of Boolean)
            ' The serverUri parameter should start with the ftp:// scheme.
            If ftpUri.Scheme <> Uri.UriSchemeFtp Then
                Return False
            End If

            'check if file already exists on disk
            If File.Exists(myfile) Then
            End If

            ' Get the object used to communicate with the server.
            Dim client As New WebClient()

            ' This example assumes the FTP site uses anonymous logon.
            client.Credentials = New NetworkCredential("anonymous", "")

                Return Await Task.Run(Function()
                                          Dim newFileData() As Byte = client.DownloadData(ftpUri.ToString())
                                          Dim fileString As String = Encoding.UTF8.GetString(newFileData)

                                          Dim sw = New StreamWriter(myfile, True, Encoding.UTF8)

                                      End Function)
            Catch e As WebException
            End Try

            Return True
        End Function

        Function EnumToList(Of T)() As IEnumerable(Of T)
            Dim enumType As Type = GetType(T)
            If enumType.BaseType <> GetType([Enum]) Then Throw New ArgumentException("T must be of type System.Enum")
            Dim enumValArray As Array = [Enum].GetValues(enumType)
            Dim enumValList As List(Of T) = New List(Of T)(enumValArray.Length)
            For Each val As Integer In enumValArray
                enumValList.Add(CType([Enum].Parse(enumType, val.ToString()), T))

            Return enumValList
        End Function

        Enum StationEnum
        End Enum
    End Module

    Marilyn Gambone

    • Edited by deskcheck1 Wednesday, April 4, 2018 8:08 PM Function name error
    • Moved by Hart Wang Friday, April 6, 2018 2:26 AM
    Wednesday, April 4, 2018 8:06 PM


All replies

  • Hi,

    Thank you for posting here.

    Since your issue is related to VB issue, I will move the case to vb forum.

    Best Regards,


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Friday, April 6, 2018 2:24 AM
  • This was answered by dbasnett in another post:

    Marilyn Gambone

    • Marked as answer by deskcheck1 Friday, April 6, 2018 4:46 PM
    Friday, April 6, 2018 4:46 PM