FtpClient.EndOpenWrite Method

System.Net.FtpClient

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

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

Syntax

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

Parameters

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

Return Value

Type: Stream
A writable stream

Implements

IFtpClient..::..EndOpenWrite(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 BeginOpenWriteExample {
        static ManualResetEvent m_reset = new ManualResetEvent(false);

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

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

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

        static void BeginOpenWriteCallback(IAsyncResult ar) {
            FtpClient conn = ar.AsyncState as FtpClient;
            Stream istream = null, ostream = null;
            byte[] buf = new byte[8192];
            int read = 0;

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

                ostream = conn.EndOpenWrite(ar);
                istream = new FileStream("input_file", FileMode.Open, FileAccess.Read);

                while ((read = istream.Read(buf, 0, buf.Length)) > 0) {
                    ostream.Write(buf, 0, read);
                }
            }
            catch (Exception ex) {
                Console.WriteLine(ex.ToString());
            }
            finally {
                if (istream != null)
                    istream.Close();

                if (ostream != null)
                    ostream.Close();

                m_reset.Set();
            }
        }
    }
}

See Also