Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
删除部分取消的复制文件VB.NET_.net_Vb.net_File_Stream_File Copying - Fatal编程技术网

删除部分取消的复制文件VB.NET

删除部分取消的复制文件VB.NET,.net,vb.net,file,stream,file-copying,.net,Vb.net,File,Stream,File Copying,删除在复制过程中被取消的文件的最佳方法是什么?我所做的是,如果单击取消按钮,将启用计时器,并检查文件是否存在,如果存在,则将删除文件,然后禁用计时器,反之亦然。这是我的密码: Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click Timer3.Enabled = True End Sub updatedFiles是一

删除在复制过程中被取消的文件的最佳方法是什么?我所做的是,如果单击
取消
按钮,将启用
计时器
,并检查文件是否存在,如果存在,则将删除文件,然后禁用计时器,反之亦然。这是我的密码:

Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
    Timer3.Enabled = True 
End Sub
updatedFiles
是一个
列表(字符串)
,其中包含在复制之前放在那里的文件

Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
    Dim dest As String = Label6.Text
        For i = 0 To updatedFilesCancel.Count - 1
             File.Delete(Path.Combine(dest, updatedFilesCancel(i)))
             Timer3.Enabled = False
        Next
End Sub

计时器的时间为1秒。间隔,它成功删除一次,第二次不再工作,出现什么问题?建议?

我建议您将一起使用,而不是干扰计时器,它将为您提供更好的文件复制操作控制和行为

我在一个名为的简单类中实现了所有这些,该类用于演示一个示例

如果您不喜欢它,您可以使用源代码并根据您的需要对其进行调整,或者看看如何管理我的源代码中的内容,使之成为您自己的

例如:

Imports Elektro.IO.Types

Public Class Form1 : Inherits Form

    Friend WithEvents FileCopier As FileCopy
    Private fileCopyCancelToken As CancellationTokenSource

    Private Sub ButtonStart_Click() Handles Button1.Click

        Me.FileCopier = New FileCopy("C:\Source File.ext")

        ' It returns a CancellationTokenSource to cancel the task whenever you want.
        Me.fileCopyCancelToken = Me.FileCopier.CopyTo("C:\Target file.ext",
                                                      overwrite:=True,
                                                      deleteFileOnCancel:=True,
                                                      cancelCallback:=Nothing)

    End Sub

    Private Sub ButtonCancel_Click() Handles Button2.Click

        Me.fileCopyCancelToken.Cancel()

    End Sub

    Private Sub FileCopier_ProgressChanged(ByVal sender As Object, ByVal e As FileCopyProgressChangedEventArgs) _
    Handles FileCopier.FileCopyProgressChanged

        Console.WriteLine(String.Format("Copied: {0:0.00}%", e.Percentage))

    End Sub

End Class
另一个(更扩展的)示例可以在源代码中找到:

Public Class Form1 : Inherits Form

    Friend WithEvents FileCopier As FileCopy
    Private fileCopyCancelToken As CancellationTokenSource

    Private Sub Button1_Click() Handles Button1.Click

        Me.StartCopy()

    End Sub

    Private Sub Button2_Click() Handles Button2.Click

        Me.CancelCopy()

    End Sub

    Private Sub StartCopy()

        ' Create a dummy file of 2 GB
        Using fs As New FileStream("C:\source file.ext", FileMode.CreateNew)
            fs.SetLength(2147483648)
        End Using

        Me.FileCopier = New FileCopy("C:\source file.ext")

        Me.fileCopyCancelToken =
            Me.FileCopier.CopyTo("C:\Target file.ext",
                                 bufferSize:=(CInt(Me.FileCopier.File.Length \ (1024 * 100)) + 1),
                                 overwrite:=True,
                                 deleteFileOnCancel:=False,
                                 cancelCallback:=AddressOf Me.FileCopier_CancelCallBack)

    End Sub

    Private Sub CancelCopy()

        ' Cancel the current file-copy operation.
        Me.FileCopier.CancelCopy(Me.fileCopyCancelToken)

    End Sub

    ''' <summary>
    ''' Callback that is called when the a file-copy operaton of the <see cref="FileCopier"/> is cancelled.
    ''' </summary>
    Private Sub FileCopier_CancelCallBack()

        Me.Invoke(
            Sub()
                Me.Label1.Text = "Canceled!"
                Me.Label2.Text = "Canceled!"
                Me.Label3.Text = "Canceled!"
                Me.Label4.Text = "Canceled!"
            End Sub)
    End Sub

    ''' ----------------------------------------------------------------------------------------------------
    ''' <summary>
    ''' Handles the <see cref="FileCopy.FileCopyProgressChanged"/> event of the <see cref="FileCopier"/> instance.
    ''' </summary>
    ''' ----------------------------------------------------------------------------------------------------
    ''' <param name="sender">
    ''' The source of the event.
    ''' </param>
    ''' 
    ''' <param name="e">
    ''' The <see cref="FileCopyProgressChangedEventArgs"/> instance containing the event data.
    ''' </param>
    ''' ----------------------------------------------------------------------------------------------------
    Private Sub FileCopier_FileCopyProgressChanged(ByVal sender As Object, ByVal e As FileCopyProgressChangedEventArgs) _
    Handles FileCopier.FileCopyProgressChanged

        Me.Invoke(
            Sub()
                Me.Label1.Text = String.Format("Size: {0} MB", (e.Filesize / 1024).ToString("n2"))
                Me.Label2.Text = String.Format("Written: {0} MB", (e.BytesRead / 1024).ToString("n2"))
                Me.Label3.Text = String.Format("Read: {0} MB", (e.BytesRemaining / 1024).ToString("n2"))
                Me.Label4.Text = String.Format("Done: {0}%", e.Percentage.ToString("n2"))
            End Sub)

    End Sub

End Class
公共类Form1:继承表单
Friend WithEvents FileCopier作为FileCopy
Private fileCopyCancelToken作为CancellationTokenSource
私有子按钮1\u Click()处理按钮1。单击
Me.StartCopy()
端接头
私有子按钮2\u Click()处理按钮2。单击
我
端接头
私有子StartCopy()
'创建一个2 GB的虚拟文件
将fs用作新的文件流(“C:\source file.ext”,FileMode.CreateNew)
fs.SetLength(2147483648)
终端使用
Me.FileCopier=newfilecopy(“C:\source file.ext”)
Me.fileCopyCancelToken=
Me.FileCopier.CopyTo(“C:\Target file.ext”,
缓冲区大小:=(CInt(Me.FileCopier.File.Length\(1024*100))+1),
覆盖:=真,
deleteFileOnCancel:=False,
cancelCallback:=AddressOf Me.FileCopier\u cancelCallback)
端接头
私有子副本()
'取消当前的文件复制操作。
Me.FileCopier.CancelCopy(Me.fileCopyCancelToken)
端接头
''' 
取消的a文件复制操作时调用的“”回调。
''' 
专用子文件复制程序\u CancelCallBack()
我,调用(
分()
Me.Label1.Text=“已取消!”
Me.Label2.Text=“取消!”
Me.Label3.Text=“取消!”
Me.Label4.Text=“已取消!”
末端接头)
端接头
''' ----------------------------------------------------------------------------------------------------
''' 
''处理实例的事件。
''' 
''' ----------------------------------------------------------------------------------------------------
''' 
''事件的源头。
''' 
''' 
''' 
''包含事件数据的实例。
''' 
''' ----------------------------------------------------------------------------------------------------
私有子文件复制器\u FileCopyProgressChanged(ByVal发件人作为对象,ByVal e作为FileCopyProgressChangedEventArgs)_
处理FileCopier.FileCopyProgressChanged
我,调用(
分()
Me.Label1.Text=String.Format(“大小:{0}MB”,(e.Filesize/1024).ToString(“n2”))
Me.Label2.Text=String.Format(“writed:{0}MB”,(e.BytesRead/1024).ToString(“n2”))
Me.Label3.Text=String.Format(“读取:{0}MB”,(e.byteslaining/1024).ToString(“n2”))
Me.Label4.Text=String.Format(“完成:{0}%”),e.Percentage.ToString(“n2”))
末端接头)
端接头
末级

我希望这能帮助你。

你为什么需要计时器?嗯,这是我唯一能想到的办法。因为我正在使用
FileStream
来复制文件,如果我在取消复制后删除文件,则会抛出一个错误
文件正在使用
,因此我等待
FileStream
完成其过程,然后再删除。我的操作方法如下:
如果file.Exists(“path”)存在,那么file.delete(“path”)
取消与否的区别是什么?如果文件计数不在100秒之内,请不要担心计时器或线程问题。只需将计时器代码移到“取消”按钮代码中,然后清除计时器。如果文件计数足够高,则需要花费大量时间,然后使用
线程
BackgroundWorker
控件。