简体   繁体   中英

How to overwrite downloaded file vb.net

I'm making a automatic file downloader and I need it to redownload and overwrite the file, when i press the button.

Here is my code:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  My.Computer.Network.DownloadFile _
    ("http://www.randomurl.com/randomfile.txt", _
    Path.Combine(Environment.GetFolderPath( _
    Environment.SpecialFolder.ApplicationData), _
    "test/randomfile.txt"))
End Sub

There is an overload for DownloadFile that allows the overwrite of the previous file

 My.Computer.Network.DownloadFile 
       (address, destinationFileName, userName,
        password, showUI, connectionTimeout, overwrite)

As from MSDN

  • address = String or Uri. Path of the file to download, including file name and host address. Required.
  • destinationFileName = String. File name and path of the downloaded file. Required.
  • userName = String. User name to authenticate. Default is an empty string, "".
  • password = String.Password to authenticate. Default is an empty string, "".
  • showUI = Boolean.Specifies whether to display the progress of the operation. Default is False.
  • connectionTimeout = Int32. Timeout interval, in milliseconds. Default is 100 seconds.
  • overwrite = Boolean. Specifies whether to overwrite existing files. Default is False.

Thus you could change your code in this way

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  My.Computer.Network.DownloadFile _
    (address := "http://www.randomurl.com/randomfile.txt", _
    destinationFileName := Path.Combine(Environment.GetFolderPath( _
    Environment.SpecialFolder.ApplicationData), _
    "test/randomfile.txt"), _
    userName := string.Empty, password := string.Empty, _
    showUI := False, connectionTimeout := 100000, _
    overwrite := True)
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim uri As System.Uri = New System.Uri("http://www.randomurl.com/randomfile.txt")
    Dim webclient As System.Net.WebClient = New System.Net.WebClient()

    Dim path As String = New String(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "test\\randomfile.txt"))
    Dim fileInfo As System.IO.FileInfo = New System.IO.FileInfo(path)
    If Not System.IO.Directory.Exists(fileInfo.Directory.FullName) Then
        System.IO.Directory.CreateDirectory(fileInfo.Directory.FullName)
    End If

    AddHandler webclient.DownloadFileCompleted, AddressOf webclient_DownloadDataCompleted

    webclient.DownloadFileAsync(uri, path)

End Sub


Private Sub webclient_DownloadDataCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)

    MessageBox.Show("Your download has completed.")

End Sub

(EDIT - changed to show async method as requested in comments)

Note that the file will be overwritten if it exists -> http://msdn.microsoft.com/en-us/library/ez801hhe(v=VS.80).aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM