只能下载一个文件

时间:2017-03-19 20:58:38

标签: vb.net winforms

我在第一个文件完成后尝试下载第二个文件时遇到了问题。

这是错误:

  

System.IO.IOException:'无法创建“C:\ Users \ user \ Desktop \ d.zip”,因为已经存在同名的文件或目录。'

此文件不存在,因此不知道发生了什么。

代码:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim req As System.Net.WebRequest
    Dim resp As System.Net.WebResponse
    req = Net.WebRequest.Create("https://filedownloads.000webhostapp.com/RedAlert1_Online.rar")
    resp = req.GetResponse
    req.Method = Net.WebRequestMethods.Http.Get
    download_size = resp.ContentLength
    ProgressBar1.Maximum = download_size

    File_name = InputBox("Please Enter File Name!")
    File_save = File_save + "\" + File_name + ".zip"

    If File_name.Length > 0 Then
        My.Computer.Network.DownloadFile("https://filedownloads.000webhostapp.com/RedAlert1_Online.rar", (File_save), "", "", False, 360000, True)
        Timer1.Start()
        Button1.Hide()
    End If
End Sub

Dim download_size As Long
Dim downloaded_size As Long

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    downloaded_size = My.Computer.FileSystem.GetFileInfo(File_save).Length
    ProgressBar1.Value = downloaded_size
End Sub

我确实有另外一段代码与此相同,我刚刚更改了块中的第二次下载,它会启动进度条,定时器等。

1 个答案:

答案 0 :(得分:1)

请在下载前检查文件是否已存在。我无法在评论中显示代码,因此将其写为答案。顺便说一下,url的扩展名为.rar,而File_save的扩展名为.zip

If File_name.Length > 0 Then
    If File.Exists(File_save) Then
        MessageBox.Show("File already exist")
    Else
        My.Computer.Network.DownloadFile("https://filedownloads.000webhostapp.com/RedAlert1_Online.rar", (File_save), "", "", False, 360000, True)
        Timer1.Start()
        Button1.Hide()
    End If
End If