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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15325144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:42:52  来源:igfitidea点击:

How to overwrite downloaded file vb.net

vb.net

提问by TheCreepySheep

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

回答by Steve

There is an overload for DownloadFilethat 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. 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.
  • 地址 = 字符串或 Uri。要下载的文件的路径,包括文件名和主机地址。必需的。
  • 目标文件名 = 字符串。下载文件的文件名和路径。必需的。
  • 用户名 = 字符串。要验证的用户名。默认为空字符串“”。
  • 密码 = String.Password 进行身份验证。默认为空字符串“”。
  • showUI = Boolean.指定是否显示操作进度。默认值为假。
  • 连接超时 = Int32。超时间隔,以毫秒为单位。默认值为 100 秒。
  • 覆盖 = 布尔值。指定是否覆盖现有文件。默认值为假。

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

回答by GojiraDeMonstah

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/en-us/library/ez801hhe(v=VS.80).aspx