Stream.CopyTo - How do I get the sent Bytes?

后端 未结 2 1999
遇见更好的自我
遇见更好的自我 2021-01-28 07:09


I try to get the transfer speed at a ftp-upload, but I don\'t know where I should \"get\" it:

Code-Snippet:

FtpWebRequest requ         


        
2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-28 07:33

    You already have a CopyStream method, just need to improve performance. BufferedStream is great for this. See below.

    I believe You can also improve it further by using the Async methods in .net 4.

    public static void CopyStream(Stream input, Stream output, Action totalSent)
    {
        BufferedStream inputBuffer = new BufferedStream(input);
        BufferedStream outputBuffer = new BufferedStream(output);
        byte[] buffer = new byte[32768];
        int read;
        int total = 0;
        while ((read = inputBuffer.Read(buffer, 0, buffer.Length)) > 0)
        {
             outputBuffer.Write (buffer, 0, read);
             total += read;
             totalSent(total);
        }
        outputBuffer.Flush();
    }
    

提交回复
热议问题