开发者

FileInfo.Length always show a files total size, not actual current size

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 ans开发者_开发问答wer 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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜