2016-01-24 57 views
0

我正在使用和陣列的文件從一個文件夾複製到另一個文件夾,但它給了我一個錯誤。
WebClient does not support concurrent I/O operations.
WebClient錯誤與陣列VB.NET

這是我的代碼:

Protected Overrides Sub OnLoad(e As EventArgs) 
    MyBase.OnLoad(e) 
    CopyBtn.Text = "Copy File" 
    CopyBtn.Parent = Me 
    ProgBar.Left = CopyBtn.Right 
End Sub 

Dim WithEvents CopyBtn As New Button 
Dim ProgBar As New ProgressBar 
Dim WithEvents FileCopier As New WebClient 

Private Sub CopyBtn_Click(sender As Object, e As EventArgs) Handles CopyBtn.Click 

    Dim src As String = "D:\test" 
    Dim dest As String = "D:\test2" 

    Dim filesToCopy As New ArrayList() 
    For Each Dir As String In System.IO.Directory.GetFiles(src) 
     Dim dirInfo As New System.IO.DirectoryInfo(Dir) 
     If Not System.IO.File.Exists(dest & "\" & dirInfo.Name) Then 
      filesToCopy.Add(dirInfo.Name) 
     End If 
    Next 

    If filesToCopy.Count > 0 Then 
     If MsgBox("There are new files found. Do you want to sync it now?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Confirm") = MsgBoxResult.Yes Then 
      For i = 0 To filesToCopy.Count - 1 
       CopyBtn.Enabled = False 
       ProgBar.Parent = Me 
       FileCopier.DownloadFileAsync(New Uri(src & "\" & filesToCopy(i)), dest & "\" & filesToCopy(i)) 
      Next 
     End If 
    Else 
     MsgBox("No new files to be copied") 
    End If 

End Sub 
Private Sub FileCopier_DownloadProgressChanged(sender As Object, e As DownloadProgressChangedEventArgs) Handles FileCopier.DownloadProgressChanged 
    Dim bytesIn As Double = Double.Parse(e.BytesReceived.ToString()) 
    Dim totalBytes As Double = Double.Parse(e.TotalBytesToReceive.ToString()) 
    Dim percentage As Double = bytesIn/totalBytes * 100 
    ProgBar.Value = Int32.Parse(Math.Truncate(percentage).ToString()) 
End Sub 
Private Sub FileCopier_DownloadFileCompleted(sender As Object, e As System.ComponentModel.AsyncCompletedEventArgs) Handles FileCopier.DownloadFileCompleted 
    ProgBar.Parent = Nothing 
    CopyBtn.Enabled = True 
End Sub 

但是當我把這個代碼之前複製/ downloadfileasync
Dim FileCopier as WebClient = New Webclient
它成功地複製。但進度條不起作用,
即使我把它放在DownloadProgressChanged
ProgBar.Value = e.ProgressPercentage
它不會加載。你能幫我麼?剛剛在這裏學習的新手。

回答

0

WEW,我只是需要與此代碼添加此
AddHandler FileCopier.DownloadProgressChanged, AddressOf FileCopier_DownloadProgressChanged AddHandler FileCopier.DownloadFileCompleted, AddressOf FileCopier_DownloadFileCompleted

Dim FileCopier as WebClient = New Webclient
ProgBar.Value = e.ProgressPercentage