如何stop/cancel FileStream 中的复制VB.NET
How to stop/cancel FileStream in copying VB.NET
复制多个文件时,停止整个 FileStream
操作的正确方法是什么。当我调用 Stop Download()
函数时,它停止了,但是当我再次开始复制时,它并没有像以前那样开始。我该怎么办?
代码:
Private dStop As Boolean = False
Dim streamRead As FileStream
Dim streamWrite As FileStream
Public Function StopDownload()
dStop = True
streamWrite.Flush()
streamWrite.Close()
streamRead.Close()
End Function
Public Sub Copier(ByVal URL As String, ByVal Location As String) As Boolean
streamRead = New FileStream(URL, System.IO.FileMode.Open)
streamWrite = New FileStream(Location, System.IO.FileMode.Create)
Dim lngLen As Long = streamRead.Length - 1
Dim byteBuffer(1048576) As Byte
Dim intBytesRead As Integer
While streamRead.Position < lngLen And dStop = False 'This is where i stop the process of copying
intBytesRead = (streamRead.Read(byteBuffer, 0, 1048576))
streamWrite.Write(byteBuffer, 0, intBytesRead)
Pbar1.value = CInt(streamRead.Position / lngLen * 100)
Application.DoEvents() 'do it
End While
streamWrite.Flush()
streamWrite.Close()
streamRead.Close()
End Sub
我只需要把这个放在我的 Copier
Public Sub.
开头
dstop = False
让它开始新的复制过程。
复制多个文件时,停止整个 FileStream
操作的正确方法是什么。当我调用 Stop Download()
函数时,它停止了,但是当我再次开始复制时,它并没有像以前那样开始。我该怎么办?
代码:
Private dStop As Boolean = False
Dim streamRead As FileStream
Dim streamWrite As FileStream
Public Function StopDownload()
dStop = True
streamWrite.Flush()
streamWrite.Close()
streamRead.Close()
End Function
Public Sub Copier(ByVal URL As String, ByVal Location As String) As Boolean
streamRead = New FileStream(URL, System.IO.FileMode.Open)
streamWrite = New FileStream(Location, System.IO.FileMode.Create)
Dim lngLen As Long = streamRead.Length - 1
Dim byteBuffer(1048576) As Byte
Dim intBytesRead As Integer
While streamRead.Position < lngLen And dStop = False 'This is where i stop the process of copying
intBytesRead = (streamRead.Read(byteBuffer, 0, 1048576))
streamWrite.Write(byteBuffer, 0, intBytesRead)
Pbar1.value = CInt(streamRead.Position / lngLen * 100)
Application.DoEvents() 'do it
End While
streamWrite.Flush()
streamWrite.Close()
streamRead.Close()
End Sub
我只需要把这个放在我的 Copier
Public Sub.
dstop = False
让它开始新的复制过程。