Я использую и массивы файлов, которые нужно скопировать из папки в другую папку, но это дает мне ошибку. 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
не загружается. не могли бы вы мне помочь? Просто новичок все еще учится здесь.