简体   繁体   English

如何覆盖下载的文件vb.net

[英]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 DownloadFile有一个重载,它允许覆盖先前的文件

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

As from MSDN 从MSDN

  • address = String or Uri. 地址=字符串或Uri。 Path of the file to download, including file name and host address. 要下载的文件的路径,包括文件名和主机地址。 Required. 需要。
  • destinationFileName = String. destinationFileName =字符串。 File name and path of the downloaded file. 文件名和下载文件的路径。 Required. 需要。
  • userName = String. userName =字符串。 User name to authenticate. 要验证的用户名。 Default is an empty string, "". 默认值为空字符串“”。
  • password = String.Password to authenticate. 密码= String.Password进行身份验证。 Default is an empty string, "". 默认值为空字符串“”。
  • showUI = Boolean.Specifies whether to display the progress of the operation. showUI =布尔值。指定是否显示操作进度。 Default is False. 默认值为False。
  • connectionTimeout = Int32. connectionTimeout = Int32。 Timeout interval, in milliseconds. 超时时间,以毫秒为单位。 Default is 100 seconds. 默认值为100秒。
  • overwrite = Boolean. overwrite =布尔值。 Specifies whether to overwrite existing files. 指定是否覆盖现有文件。 Default is False. 默认值为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 请注意,如果该文件存在,它将被覆盖-> http://msdn.microsoft.com/zh-cn/library/ez801hhe(v=VS.80).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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