2017-09-13 61 views
1

有誰知道如何解析一個Webclient.DowloadFile到VB.Net文件? 我目前使用下面的代碼,但我不斷收到錯誤: (BC30491)表達式不會產生一個值。類型的 (BC30311)值不能被轉換到文件WebClient.DownloadFile轉換爲文件VB.NET

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 
+0

的FUNC ['DownloadFile'](https://msdn.microsoft.com/en-us/library/ez801hhe(V = vs.110)的.aspx)將不會返回值。你可以用你存儲下載文件的路徑初始化'File' – Prisoner

回答

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