FtpClient.BeginOpenRead Method (String, FtpDataType, AsyncCallback, Object)

System.Net.FtpClient

Collapse image Expand Image Copy image CopyHover image
Opens the specified file for reading

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

Syntax

C#
public IAsyncResult BeginOpenRead(
	string path,
	FtpDataType type,
	AsyncCallback callback,
	Object state
)
Visual Basic
Public Function BeginOpenRead ( 
	path As String,
	type As FtpDataType,
	callback As AsyncCallback,
	state As Object
) As IAsyncResult
Visual C++
public:
virtual IAsyncResult^ BeginOpenRead(
	String^ path, 
	FtpDataType type, 
	AsyncCallback^ callback, 
	Object^ state
) sealed

Parameters

path
Type: System..::..String
The full or relative path of the file
type
Type: System.Net.FtpClient..::..FtpDataType
ASCII/Binary
callback
Type: System..::..AsyncCallback
Async Callback
state
Type: System..::..Object
State object

Return Value

Type: IAsyncResult
IAsyncResult

Implements

IFtpClient..::..BeginOpenRead(String, FtpDataType, AsyncCallback, Object)

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