FtpClient.EndOpenRead Method

System.Net.FtpClient

Collapse image Expand Image Copy image CopyHover image
Ends a call to BeginOpenRead()

Namespace: System.Net.FtpClient
Assembly: System.Net.FtpClient (in System.Net.FtpClient.dll) Version: 1.0.5064.17461

Syntax

C#
public Stream EndOpenRead(
	IAsyncResult ar
)
Visual Basic
Public Function EndOpenRead ( 
	ar As IAsyncResult
) As Stream
Visual C++
public:
virtual Stream^ EndOpenRead(
	IAsyncResult^ ar
) sealed

Parameters

ar
Type: System..::..IAsyncResult
IAsyncResult returned from BeginOpenRead()

Return Value

Type: Stream
A readable stream

Implements

IFtpClient..::..EndOpenRead(IAsyncResult)

Examples

C#  Copy imageCopy
using System;
using System.Net;
using System.Net.FtpClient;
using System.IO;
using System.Threading;

namespace Examples {
    public static class BeginOpenReadExample {
        static ManualResetEvent m_reset = new ManualResetEvent(false);

        public static void BeginOpenRead() {
            using (FtpClient conn = new FtpClient()) {
                m_reset.Reset();

                conn.Host = "localhost";
                conn.Credentials = new NetworkCredential("ftptest", "ftptest");
                conn.BeginOpenRead("/path/to/file",
                    new AsyncCallback(BeginOpenReadCallback), conn);

                m_reset.WaitOne();
                conn.Disconnect();
            }
        }

        static void BeginOpenReadCallback(IAsyncResult ar) {
            FtpClient conn = ar.AsyncState as FtpClient;

            try {
                if (conn == null)
                    throw new InvalidOperationException("The FtpControlConnection object is null!");

                using (Stream istream = conn.EndOpenRead(ar)) {
                    byte[] buf = new byte[8192];

                    try {
                        DateTime start = DateTime.Now;

                        while (istream.Read(buf, 0, buf.Length) > 0) {
                            double perc = 0;

                            if (istream.Length > 0)
                                perc = (double)istream.Position / (double)istream.Length;

                            Console.Write("\rTransferring: {0}/{1} {2}/s {3:p}         ",
                                          istream.Position.FormatBytes(),
                                          istream.Length.FormatBytes(),
                                          (istream.Position / DateTime.Now.Subtract(start).TotalSeconds).FormatBytes(),
                                          perc);
                        }
                    }
                    finally {
                        Console.WriteLine();
                        istream.Close();
                    }
                }
            }
            catch (Exception ex) {
                Console.WriteLine(ex.ToString());
            }
            finally {
                m_reset.Set();
            }
        }
    }
}

See Also