如何覆盖vb.net下载的文件
我正在制作一个自动文件下载程序,当我按下按钮时,我需要它来重新下载和覆盖文件 这是我的密码:如何覆盖vb.net下载的文件,vb.net,Vb.net,我正在制作一个自动文件下载程序,当我按下按钮时,我需要它来重新下载和覆盖文件 这是我的密码: 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.GetFo
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
存在允许覆盖上一个文件的重载
My.Computer.Network.DownloadFile
(address, destinationFileName, userName,
password, showUI, connectionTimeout, overwrite)
从MSDN开始
- 地址=字符串或Uri。要下载的文件的路径,包括 姓名和主机地址。必需的
- destinationFileName=字符串。下载文件的文件名和路径 文件必需的
- 用户名=字符串。要验证的用户名。默认值为空 字符串“”
- password=String。用于身份验证的密码。默认值为空字符串, “”
- showUI=Boolean。指定是否显示项目的进度 活动默认值为False
- connectionTimeout=Int32。超时间隔,以毫秒为单位。违约 是100秒
- 覆盖=布尔值。指定是否覆盖现有文件。 默认值为False
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