WebClient.DownloadFile转换为File VB.NET

时间:2017-09-13 07:24:19

标签: vb.net webclient

有谁知道如何在VB.Net中解析Webclient.DowloadFile到File? 我目前正在使用以下代码,但我不断收到错误: (BC30491)表达式不会产生值。 (BC30311)类型的值无法转换为文件

Private Function DepartmentDataDownloader() As String
    Dim webClient As New System.Net.WebClient
    'I get here the error BC30491
    Dim result As File = webClient.DownloadFile("https://intern.hethoutsemeer.nl/index2.php?option=com_webservices&controller=csv&method=hours.board.fetch&key=F2mKaXYGzbjMA4V&element=departments", "intern.hethoutsemeer.nl.1505213278-Departments.csv")

    If webClient IsNot Nothing Then
        'and here BC30311
        Dim result As File = webClient
        UploaderFromDownload(webClient)
    End If
    Return ""
End Function

1 个答案:

答案 0 :(得分:1)

DownloadFile方法没有返回值,因为DownloadFileSub。这就是您收到BC30491错误的原因。第二个参数指定本地文件的路径(以及下载结果)。

所以你可以尝试以下内容:

Dim webClient As New System.Net.WebClient

'after this the file specified on second parameter should exists.
webClient.DownloadFile("https://intern.hethoutsemeer.nl/index2.php?option=com_webservices&controller=csv&method=hours.board.fetch&key=F2mKaXYGzbjMA4V&element=departments", "intern.hethoutsemeer.nl.1505213278-Departments.csv")

If webClient IsNot Nothing Then

    'do something with the downloaded file (File.OpenRead(), File.*).
    'Dim result As File
    UploaderFromDownload(webClient)
End If

您还尝试将WebClient值分配给File变量。这是不可能的,因此您会收到BC30311错误。

  

提示:您无法创建File类的实例。 File类提供用于创建,复制,删除,移动和打开单个文件的静态方法,并帮助创建FileStream个对象。
  来源: https://docs.microsoft.com/en-us/dotnet/api/system.io.file