Skip to main content

 none
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 = "ftp://ftp.ncdc.noaa.gov/pub/data/ghcn/daily/all/" & 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)
            Next
        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
                File.Delete(myfile)
            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", "janeDoe@contoso.com")

            Try
                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)
                                          sw.Write(fileString)
                                          sw.Flush()
                                          sw.Close()

                                          Console.WriteLine("Done")
                                      End Function)
            Catch e As WebException
                Console.WriteLine(e.ToString())
            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))
            Next

            Return enumValList
        End Function

        Enum StationEnum
            ACW00011604
            ACW00011647
            BDM00078016
            BDW00013602
            BF000078073
            BFW00012712
        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

Answers

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,

    Hart


    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 MSDNFSF@microsoft.com.

    Friday, April 6, 2018 2:24 AM
  • This was answered by dbasnett in another post:  https://social.msdn.microsoft.com/Forums/en-US/ba4e0df2-ad42-4be8-9421-04b288340d5a/convert-to-parallel-foreach-loop-vbnet?forum=vbgeneral


    Marilyn Gambone

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