一小部分复制文件

问题描述 投票:1回答:2

我想一小部分地复制文件(如果需要,可取消复制操作。)>

我正在尝试遵循未标记的解决方案:How to copy a file with the ability to cancel the copy?

但是我得到一个0字节的文件

我做错了什么?

Public Class Form1

   Dim cancelled As Boolean = Nothing
   Dim input = New System.IO.FileStream("C:\1.txt", System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read)
   Dim output = New System.IO.FileStream("C:\Nueva carpeta\1.txt", System.IO.FileMode.CreateNew, System.IO.FileAccess.Write, System.IO.FileShare.Write)

   Public Sub CopyStream(ByVal inputStream As System.IO.Stream, ByVal outputStream As System.IO.Stream)
       'Dim buffer = System.IO.File.ReadAllBytes("C:\1.txt")

       Dim buffer = New Byte((1024) - 1) {}
       Dim bytesRead As Integer = 1

       While (inputStream.Read(buffer, 0, buffer.Length) > 0)
           outputStream.Write(buffer, 0, bytesRead)
           'bytesRead += 1

           If cancelled Then
               MsgBox("operacion cancelada")
               Return
           End If
       End While

       inputStream.Close()
       outputStream.Close()
       MsgBox("operacion terminada")

   End Sub

   Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
       CopyStream(input, output)
   End Sub

End Class

**

更新1:

**

我尝试按照Virtlink回答的步骤进行操作,并将缺少的部分放入原始代码中,但仍然得到零字节文件。

> Public Class Form1

    Dim input = New System.IO.FileStream("C:\Test.txt", System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read)
    Dim output = New System.IO.FileStream("C:\Test_New.txt", System.IO.FileMode.CreateNew, System.IO.FileAccess.Write, System.IO.FileShare.Write)

    Public Sub CopyStream(ByVal inputStream As System.IO.Stream, ByVal outputStream As System.IO.Stream)

        Dim buffer = New Byte(1024) {}
        Dim bytesRead As Integer

        ' Read some bytes
        While (bytesRead = inputStream.Read(buffer, 0, buffer.Length) > 0)
            ' Write them to the output
            outputStream.Write(buffer, 0, bytesRead)
        End While

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        CopyStream(input, output)
    End Sub

End Class

**

更新2:

**

我最新失败的尝试:

    Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim input_filepath As String = "C:\Test.txt", output_filepath As String = "C:\Test_New.txt"

        Dim input = New System.IO.FileStream(input_filepath, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite)
        Dim output = New System.IO.FileStream(output_filepath, System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite)

        CopyStream(input, output)

        ' For Testing:
        If New IO.FileInfo(output_filepath).Length = 0 Then IO.File.Delete(output_filepath) : Application.Exit() Else Process.Start("Notepad", output_filepath)

    End Sub

    Public Sub CopyStream(ByVal inputStream As System.IO.Stream, ByVal outputStream As System.IO.Stream)

        Dim buffer = New Byte(1024) {}, bytesRead As Integer

        While ((bytesRead = inputStream.Read(buffer, 0, buffer.Length)) > 0)
            outputStream.Write(buffer, 0, bytesRead)
        End While

        inputStream.Flush() : outputStream.Flush()
        inputStream.Close() : outputStream.Close()

    End Sub

End Class

**

更新3:

**

解决方案

问题出在VB.NET中,我无法在循环条件下将值分配给变量,所以这是有效的Sub:

  Public Sub CopyStream(ByVal inputStream As Stream, ByVal outputStream As Stream)

    Dim buffer = New Byte(1025) {}
    Dim bytesRead As Integer = 0

    Do
        bytesRead = inputStream.Read(buffer, 0, buffer.Length)
        If bytesRead > 0 Then
            outputStream.Write(buffer, 0, bytesRead)
        End If
    Loop While (bytesRead > 0)

    outputStream.Flush()
    inputStream.Close() : outputStream.Close()

End Sub

我想一小段地复制文件(如果需要,可以取消复制操作)。我在这里尝试使用未标记的解决方案:如何复制文件并可以取消复制?但是我是...

.net vb.net visual-studio file-io chunks
2个回答
6
投票

您必须了解您的工作及其工作方式。


0
投票

有人可以帮我吗。我的输出未合并输入的视频ts文件

© www.soinside.com 2019 - 2024. All rights reserved.