数组 VB.NET 的 WebClient 错误
WebClient error with Arrays VB.NET
我正在使用和将文件数组从一个文件夹复制到另一个文件夹,但它给了我一个错误。
WebClient does not support concurrent I/O operations.
这是我的代码:
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
但是当我把这段代码放在 copying/downloadfileasync 之前时
Dim FileCopier as WebClient = New Webclient
它成功复制。但是进度条不工作,
即使我把它放在 DownloadProgressChanged
ProgBar.Value = e.ProgressPercentage
它也不会加载。你能帮我么?新手还在学习中
哇,我只需要添加这个
AddHandler FileCopier.DownloadProgressChanged, AddressOf FileCopier_DownloadProgressChanged
AddHandler FileCopier.DownloadFileCompleted, AddressOf FileCopier_DownloadFileCompleted
使用此代码:
Dim FileCopier as WebClient = New Webclient
ProgBar.Value = e.ProgressPercentage
我正在使用和将文件数组从一个文件夹复制到另一个文件夹,但它给了我一个错误。WebClient does not support concurrent I/O operations.
这是我的代码:
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
但是当我把这段代码放在 copying/downloadfileasync 之前时
Dim FileCopier as WebClient = New Webclient
它成功复制。但是进度条不工作,
即使我把它放在 DownloadProgressChanged
ProgBar.Value = e.ProgressPercentage
它也不会加载。你能帮我么?新手还在学习中
哇,我只需要添加这个
AddHandler FileCopier.DownloadProgressChanged, AddressOf FileCopier_DownloadProgressChanged
AddHandler FileCopier.DownloadFileCompleted, AddressOf FileCopier_DownloadFileCompleted
使用此代码:Dim FileCopier as WebClient = New Webclient
ProgBar.Value = e.ProgressPercentage