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

System.Net.FtpClient

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

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

Syntax

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

Parameters

path
Type: System..::..String
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..::..BeginOpenWrite(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 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