FileInfo.Length 始终显示文件总大小,而不是实际的当前大小

发布于 2024-12-01 07:07:31 字数 553 浏览 0 评论 0原文

我正在尝试使用 File.Copy([FileName]) 命令监视大文件复制过程的进度(无需自己手动复制数据字节)。

所以我想做的是获取正在复制的文件的长度,并将其与目标文件的长度进行比较。这会让我对复制进度有一个很好的了解。

唯一的问题是目标文件上的 FileInfo.Length 属性返回正在复制的文件的总大小,而不是磁盘上文件的实际大小。还有其他方法可以获取这些数据吗? 谢谢, 克里斯



EDIT (Moved from below -- was submitted as an answer by OP)

我查看了链接,这根本不是我想要的。我只想在文件很大时显示文件复制的进度(复制时间超过 3 秒)。

大多数文件大约需要 1/4 秒,但也有一些文件大约有 600+MB,通过网络复制需要一些时间。

我所需要的只是如何获取目标目录中部分复制的文件的实际文件大小。

(由于我在这里没有帐户,所以它不允许我发布对您对我的问题的答复的答复,因此它被显示为答案,即使它不是。)

谢谢,克里斯

I am trying to monitor the progress of a large file copy proceedure (without manually copying the bytes of data myself) using the File.Copy([FileName]) command.

So what I am trying to do is get the length of the file being copied, and compare that to the length of the destination file. This would give me a good sense of the copy progress.

The only problem is that the FileInfo.Length property on the destination file returns the total size of the file being copied, not the actual size of the file on the disk. Is there any other way to get this data?
Thanks,
Chris


EDIT (Moved from below -- was submitted as an answer by OP)

I looked at the link, and that is not at all what I want. I only want to show the progress of the file copy if the file is large (taking more than 3 seconds to copy).

The majority of the files take about 1/4 of a second, but there are a few that are ~600+MB, which take some time to copy over the network.

All I need is how to get the actual file size of the partially copied file in the destination directory.

(Since I don't have an account here, it did not let me post a reply to your reply to my question, so it is being shown as an answer, even though it is not.)

Thanks, Chris

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岛歌少女 2024-12-08 07:07:31

您可以使用流自行执行复制操作并显示进度:

    Const SOURCE_FILEPATH = "C:\TEMP\GameOfThrones.png"
    Const DEST_FILEPATH = "C:\GameOfThrones.png"

    Const BUFFER_SIZE As Integer = 32767
    If System.IO.File.Exists(DEST_FILEPATH) Then System.IO.File.Delete(DEST_FILEPATH)
    Using inStream As New System.IO.FileStream(SOURCE_FILEPATH, IO.FileMode.Open, IO.FileAccess.Read)
        Dim offset As Integer = 0
        Dim count As Integer = BUFFER_SIZE
        Using outStream As New System.IO.FileStream(DEST_FILEPATH, IO.FileMode.Create, IO.FileAccess.Write)
            Do While offset + count <= inStream.Length AndAlso offset <> inStream.Length
                Dim buffer(count) As Byte
                inStream.Seek(offset, IO.SeekOrigin.Begin)
                inStream.Read(buffer, 0, buffer.Length - 1)
                outStream.Seek(outStream.Length, IO.SeekOrigin.Begin)
                outStream.Write(buffer, 0, buffer.Length - 1)
                offset += count
                If count + offset > inStream.Length Then
                    count = inStream.Length - offset
                Else
                    count = BUFFER_SIZE
                End If
                System.Console.WriteLine((offset / inStream.Length) * 100 & "% complete")
            Loop
        End Using
    End Using

You can do the copy operation yourself using streams and display the progress:

    Const SOURCE_FILEPATH = "C:\TEMP\GameOfThrones.png"
    Const DEST_FILEPATH = "C:\GameOfThrones.png"

    Const BUFFER_SIZE As Integer = 32767
    If System.IO.File.Exists(DEST_FILEPATH) Then System.IO.File.Delete(DEST_FILEPATH)
    Using inStream As New System.IO.FileStream(SOURCE_FILEPATH, IO.FileMode.Open, IO.FileAccess.Read)
        Dim offset As Integer = 0
        Dim count As Integer = BUFFER_SIZE
        Using outStream As New System.IO.FileStream(DEST_FILEPATH, IO.FileMode.Create, IO.FileAccess.Write)
            Do While offset + count <= inStream.Length AndAlso offset <> inStream.Length
                Dim buffer(count) As Byte
                inStream.Seek(offset, IO.SeekOrigin.Begin)
                inStream.Read(buffer, 0, buffer.Length - 1)
                outStream.Seek(outStream.Length, IO.SeekOrigin.Begin)
                outStream.Write(buffer, 0, buffer.Length - 1)
                offset += count
                If count + offset > inStream.Length Then
                    count = inStream.Length - offset
                Else
                    count = BUFFER_SIZE
                End If
                System.Console.WriteLine((offset / inStream.Length) * 100 & "% complete")
            Loop
        End Using
    End Using
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文